Core. Socket

Socket Encapsulating HTML5 WebSocket allows full duplex (full-duplex) real-time communication between the server and the client, and allows cross domain communication. After the connection is established, the server and Browser/Client Agent can actively send or receive text and binary data to each other.

To use Socket Class method, first use the constructor new Socket Create a Socket Object. Socket Transmit and receive data asynchronously.

Constructor

new Socket(hostopt, portopt, byteClassopt)

Create new Socket Object. The default byte order is Socket.BIG_ENDIAN. If no arguments are specified, a socket that is initially broken is created. If a valid parameter is specified, try connecting to the specified host and port.

Be careful: It is strongly recommended to use Without parameters The constructor's form, and adds any event listeners and settings such as protocols properties, and then calls the connect method using the host and port arguments. This sequence ensures that all event listeners and other related processes work properly.

Parameters:
Name Type Attributes Default Description
host String <optional>
null host Server address.
port int <optional>
0 port Server port.
byteClass Class <optional>
null byteClass The Byte class used to receive and send data. If NULL, the Byte class is used, and the subclass of the Byte class can also be passed in.

Extends

Member

(static) BIG_ENDIAN :String

Default Value:
  • "bigEndian"

(static) LITTLE_ENDIAN :String

Default Value:
  • "littleEndian"

connected :Boolean

Express this Socket Is the object currently connected?.

disableInput :Boolean

No longer cache data from the server.

endian :String

The host byte order, are two different sequence of CPU stored data, including little endian and big endian.

LITTLE_ENDIAN: small endian, low address, low address, high address, high storage value.

BIG_ENDIAN: big endian addresses low storage value high, high value and low storage.

input :Object

Cached data from the server.

output :Object

Represents the data that needs to be sent to the buffer in the server.

protocols :Object

Sub protocol name. An array of protocol names, strings, or strings consisting of multiple sub protocol name strings. Assignment must be made before calling connect or connectByUrl, otherwise invalid.

Once specified, the connection can be established only if the server selects one of the sub protocols, otherwise failure to establish and distribute Event.ERROR events.

See:

Methods

cleanSocket()

Clean up socket.

close()

Close connection.

connect(host, port)

Connect to the specified host and port.

A successful connection dispatches the Event.OPEN event; the connection failure dispatches the Event.ERROR event; the connection is closed to distribute the Event.CLOSE event; data received dispatches the Event.MESSAGE event; in addition to the Event.MESSAGE parameters for the event data, other event parameters are native HTML DOM Event object.

Parameters:
Name Type Description
host String Server address.
port int Server port.

connectByUrl(url要连接的服务端)

连接到指定的服务端 WebSocket URL。 URL 类似 ws://

A successful connection dispatches the Event.OPEN event; the connection failure dispatches the Event.ERROR event; the connection is closed to distribute the Event.CLOSE event; data received dispatches the Event.MESSAGE event; in addition to the Event.MESSAGE parameters for the event data, other event parameters are native HTML DOM Event object.

Parameters:
Name Type Description
url要连接的服务端 String WebSocket URL。 URL 类似 ws:// Yourdomain:port.

event(type, dataopt) → {Boolean}

Dispatch events.
Inherited From:
Parameters:
Name Type Attributes Default Description
type String Event type.
data Object <optional>
null data (optional) callback data. Be careful: If you need to pass multiple parameters, P1, P2, P3, you can use the array structure, such as: [p1,p2,p3,...] If you need to call a single parameter P, and P is an array, you need to use a structure such as: [p] Other single parameters, P, can be passed directly to the parameter P.
Returns:
Type:
Boolean
Is there a listener for this event type? If there is a listener, the value is true, otherwise the value is false.

flush()

Send data from the buffer to the server.

hasListener(type) → {Boolean}

inspect EventDispatcher Object registers any listeners for a particular event type.
Inherited From:
Parameters:
Name Type Description
type String Event type.
Returns:
Type:
Boolean
If the specified type of listener has been registered, the value is true; otherwise, the value is false.

isMouseEvent(type) → {Boolean}

Detects whether the specified event type is a mouse event.
Inherited From:
Parameters:
Name Type Description
type String Event type.
Returns:
Type:
Boolean
If it is a mouse event, the value is true; otherwise, the value is false.

off(type, caller, listener, onceOnly) → {EventDispatcher}

from EventDispatcher Delete listeners in object.
Inherited From:
Parameters:
Name Type Description
type String Event type.
caller Object The domain of the event listener function.
listener function Event listener function.
onceOnly Boolean (optional) if the value is true, only the listeners added by the once method are removed.
Returns:
Type:
EventDispatcher
this EventDispatcher Object.

offAll(typeopt) → {EventDispatcher}

from EventDispatcher Removes all listeners that specify the type of event in the object.
Inherited From:
Parameters:
Name Type Attributes Default Description
type String <optional>
null type (optional) event type. If the value is null, all types of listeners for this object are removed.
Returns:
Type:
EventDispatcher
this EventDispatcher Object.

on(type, caller, listener, argsopt) → {EventDispatcher}

Use EventDispatcher Object registers the event listener object of the specified type so that listeners can receive event notifications.
Inherited From:
Parameters:
Name Type Attributes Default Description
type String Event type.
caller Object The domain of the event listener function.
listener function Event listener function.
args Array <optional>
null args Optional callback parameters for the event listener function.
Returns:
Type:
EventDispatcher
this EventDispatcher Object.

once(type, caller, listener, argsopt) → {EventDispatcher}

Use EventDispatcher Object registers the event listener object of the specified type so that the listener can receive the event notification, which is automatically removed after a response event.
Inherited From:
Parameters:
Name Type Attributes Default Description
type String Event type.
caller Object The domain of the event listener function.
listener function Event listener function.
args Array <optional>
null args Optional callback parameters for the event listener function.
Returns:
Type:
EventDispatcher
this EventDispatcher Object.

send(data)

Send data to server.
Parameters:
Name Type Description
data Object The data to be sent can be either String or ArrayBuffer.

Event

close

See:
  • events.Event

error

See:
  • events.Event

message

See:
  • events.Event

open

See:
  • events.Event