Socket encapsulates HTML5 WebSocket, allowing full-duplex real-time communication between server and client, and cross-domain communication. After establishing a connection, both server and Browser/Client Agent can actively send or receive text and binary data to each other.

  • To use Socket class methods, first create a Socket object using the constructor new Socket. Socket transmits and receives data asynchronously.
  • Event.OPEN event: dispatched after successful connection establishment.
  • Event.MESSAGE event: dispatched after receiving data.
  • Event.CLOSE event: dispatched after connection closed.
  • Event.ERROR event: dispatched after an error occurred.

Socket 封装了 HTML5 WebSocket,允许服务器端与客户端进行全双工(full-duplex)的实时通信,并且允许跨域通信。在建立连接后,服务器和 Browser/Client Agent 都能主动的向对方发送或接收文本和二进制数据。

  • 要使用Socket 类的方法,请先使用构造函数 new Socket 创建一个 Socket 对象。 Socket 以异步方式传输和接收数据。
  • Event.OPEN 事件:连接建立成功后调度。
  • Event.MESSAGE 事件:接收到数据后调度。
  • Event.CLOSE 事件:连接被关闭后调度。
  • Event.ERROR 事件:出现异常后调度。

层级 (查看完整内容)

构造函数

  • 参数

    • 可选host: string

      The server address.

    • 可选port: number

      The server port.

    • 可选protocols: string[]

      Subprotocol names. A string or an array of strings of subprotocol names.

    • 可选isSecure: boolean

      Whether to use the WebSocket secure protocol wss, default (false) uses the ordinary protocol ws.

    返回 Socket

    Create a new Socket object. The default byte order is Socket.BIG_ENDIAN. If no parameters are specified, a socket initially in a disconnected state will be created. If valid parameters are specified, it attempts to connect to the specified host and port.

    创建新的 Socket 对象。默认字节序为 Socket.BIG_ENDIAN 。若未指定参数,将创建一个最初处于断开状态的套接字。若指定了有效参数,则尝试连接到指定的主机和端口。

属性

disableInput: boolean = false

Whether to disable caching of data received from the server. If the transmitted data is in string format, it is recommended to set this to true to reduce binary conversion overhead.

是否禁用服务端发来的数据缓存。如果传输的数据为字符串格式,建议设置为true,减少二进制转换消耗。

input: Byte

The data received from the server.

服务端发来的缓存数据。

output: Byte

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

需要发送至服务端的缓冲区中的数据。

访问器

  • get connected(): boolean
  • 返回 boolean

    Indicates whether this Socket object is currently connected.

    表示此 Socket 对象目前是否已连接。

方法

  • 参数

    • host: string

      The server address.

    • port: number

      The server port.

    • 可选isSecure: boolean

      Whether to use the WebSocket secure protocol wss, default (false) uses the ordinary protocol ws.

    • 可选protocols: string[]

      Subprotocol names. A string or an array of strings of subprotocol names.

    返回 void

    Connect to the specified host and port.

    • Dispatches Event.OPEN on successful connection; Event.ERROR on connection failure; Event.CLOSE when the connection is closed; Event.MESSAGE when data is received. Except for Event.MESSAGE where the event parameter is the data content, other event parameters are native HTML DOM Event objects.

    连接到指定的主机和端口。

    • 连接成功派发 Event.OPEN 事件;连接失败派发 Event.ERROR 事件;连接被关闭派发 Event.CLOSE 事件;接收到数据派发 Event.MESSAGE 事件; 除了 Event.MESSAGE 事件参数为数据内容,其他事件参数都是原生的 HTML DOM Event 对象。
  • 参数

    • host: string

      The server address.

    • port: number

      The server port.

    • 可选isSecure: boolean

      Whether to use the WebSocket secure protocol wss, default (false) uses the ordinary protocol ws.

    • 可选options: IWebSocketConnectOptions

      Options for the WebSocket connection.

    返回 void

    Connect to the specified host and port.

    • Dispatches Event.OPEN on successful connection; Event.ERROR on connection failure; Event.CLOSE when the connection is closed; Event.MESSAGE when data is received. Except for Event.MESSAGE where the event parameter is the data content, other event parameters are native HTML DOM Event objects.

    连接到指定的主机和端口。

    • 连接成功派发 Event.OPEN 事件;连接失败派发 Event.ERROR 事件;连接被关闭派发 Event.CLOSE 事件;接收到数据派发 Event.MESSAGE 事件; 除了 Event.MESSAGE 事件参数为数据内容,其他事件参数都是原生的 HTML DOM Event 对象。
  • 参数

    • url: string

      The server WebSocket URL to connect to. The URL is similar to ws://yourdomain:port.

    • 可选protocols: string[]

      Subprotocol names. A string or an array of strings of subprotocol names.

    返回 void

    Connect to the specified server WebSocket URL. The URL is similar to ws://yourdomain:port.

    • Dispatches Event.OPEN on successful connection; Event.ERROR on connection failure; Event.CLOSE when the connection is closed; Event.MESSAGE when data is received. Except for Event.MESSAGE where the event parameter is the data content, other event parameters are native HTML DOM Event objects.

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

    • 连接成功派发 Event.OPEN 事件;连接失败派发 Event.ERROR 事件;连接被关闭派发 Event.CLOSE 事件;接收到数据派发 Event.MESSAGE 事件; 除了 Event.MESSAGE 事件参数为数据内容,其他事件参数都是原生的 HTML DOM Event 对象。
  • 参数

    • url: string

      The server WebSocket URL to connect to. The URL is similar to ws://yourdomain:port.

    • 可选options: IWebSocketConnectOptions

      Options for the WebSocket connection.

    返回 void

    Connect to the specified server WebSocket URL. The URL is similar to ws://yourdomain:port.

    • Dispatches Event.OPEN on successful connection; Event.ERROR on connection failure; Event.CLOSE when the connection is closed; Event.MESSAGE when data is received. Except for Event.MESSAGE where the event parameter is the data content, other event parameters are native HTML DOM Event objects.

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

    • 连接成功派发 Event.OPEN 事件;连接失败派发 Event.ERROR 事件;连接被关闭派发 Event.CLOSE 事件;接收到数据派发 Event.MESSAGE 事件; 除了 Event.MESSAGE 事件参数为数据内容,其他事件参数都是原生的 HTML DOM Event 对象。
  • 参数

    • type: string

      The type of event.

    • 可选data: any

      (Optional) Data to pass to the callback. If multiple parameters p1, p2, p3, ... need to be passed, use an array structure such as [p1, p2, p3, ...]. If a single parameter p needs to be passed and p is an array, use a structure such as [p]. For other single parameters p, you can directly pass parameter p. If data is Event.EMPTY, it means passing an Event object to the callback function. Note that it is not passing Event.TEMP, but an independent Event object.

    返回 boolean

    True if there are listeners for this event type, false otherwise.

    Dispatch an event.

    派发事件。

  • 参数

    • type: string

      The type of event.

    返回 boolean

    True if a listener of the specified type is registered, false otherwise.

    Check if the EventDispatcher object has any listeners registered for a specific type of event.

    检查 EventDispatcher 对象是否为特定事件类型注册了任何侦听器。

  • 参数

    • type: string

      The type of event.

    • listener: Function

      The listener function.

    返回 EventDispatcher

    This EventDispatcher object.

    Register an event listener object with the EventDispatcher object so that the listener receives event notifications.

    使用 EventDispatcher 对象注册指定类型的事件侦听器对象,以使侦听器能够接收事件通知。

  • 参数

    • type: string

      The type of event.

    • caller: any

      The execution scope of the event listener function.

    • listener: Function

      The listener function.

    • 可选args: any[]

      (Optional) The callback parameters of the event listener function.

    返回 EventDispatcher

    This EventDispatcher object.

    Register an event listener object with the EventDispatcher object so that the listener receives event notifications.

    使用 EventDispatcher 对象注册指定类型的事件侦听器对象,以使侦听器能够接收事件通知。

  • 参数

    • type: string

      The type of event.

    • listener: Function

      The listener function.

    返回 EventDispatcher

    This EventDispatcher object.

    Register an event listener object with the EventDispatcher object so that the listener receives event notifications. This event listener responds once and is automatically removed after the first call.

    使用 EventDispatcher 对象注册指定类型的事件侦听器对象,以使侦听器能够接收事件通知,此侦听事件响应一次后自动移除。

  • 参数

    • type: string

      The type of event.

    • caller: any

      The execution scope of the event listener function.

    • listener: Function

      The listener function.

    • 可选args: any[]

      (Optional) The callback parameters of the event listener function.

    返回 EventDispatcher

    This EventDispatcher object.

    Register an event listener object with the EventDispatcher object so that the listener receives event notifications. This event listener responds once and is automatically removed after the first call.

    使用 EventDispatcher 对象注册指定类型的事件侦听器对象,以使侦听器能够接收事件通知,此侦听事件响应一次后自动移除。

  • 参数

    • data: string

      The data to be sent.

    返回 Promise<void>

    Send data to the server.

    发送数据到服务器。

  • 参数

    • buffer: ArrayBuffer

      The data to be sent.

    返回 Promise<void>

    Send data to the server.

    发送数据到服务器。