aboutsummaryrefslogtreecommitdiff
path: root/docs/web-socket.md
blob: f3fc58cd1178f503f42bc059148c4b2f4e2d1fb1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
ZJS API for Web Sockets
=======================

* [Introduction](#introduction)
* [Web IDL](#web-idl)
* [API Documentation](#api-documentation)
* [Sample Apps](#sample-apps)

Introduction
------------
The Web Socket API is modeled after Node.js' 'ws' module. This module only
supports the Web Socket server portion of the API.

Web IDL
-------
This IDL provides an overview of the interface; see below for documentation of
specific API functions.

```javascript
// require returns a WebSocket object
// var ws = require('ws');

interface WebSocket {
    WebSocketServer Server(Object options);
};

interface WebSocketServer: EventEmitter;

interface WebSocketConnection: EventEmitter {
    // WebSocketConnection methods
    void send(Buffer data, Boolean mask);
    void ping(Buffer data, Boolean mask);
    void pong(Buffer data, Boolean mask);
};
```

WebSocket API Documentation
---------------------------

### WebSocket.Server
`WebSocketServer Server(Object options)`

Create a Web Socket server object. Options object may contain:

WebSocketServer API Documentation
---------------------------------

WebSocketServer is [EventEmitter](./events.md) with the following events:

### Event: 'connection'

* `WebSocketConnection` `conn`

Emitted when a client has connected to the server. The argument to any
registered listener will be a `WebSocketConnection` object which can be used to
communicate with the client.
```
{
    port : Port to bind to
    backlog : Max number of concurrent connections
    clientTracking : enable client tracking
    maxPayload : set the max payload bytes per message
    acceptHandler : handler to call to accept/deny connections
}
```
The `acceptHandler` property sets a function handler to be called when there is
a new connection. The argument will be an array of sub-protocols (Strings) that
the client is requesting to use. To accept the connection, return one of these
strings from the handler.

Returns a `WebSocketServer` object.

WebSocketConnection API Documentation
-------------------------------------

WebSocketServer is [EventEmitter](./events.md) with the following events:

### Event: 'close'

Emitted when the web socket has closed.

### Event: 'error'

* `Error` `err`

Emitted when the web socket has an error. They type of error can be found in
the `err` object argument.

### Event: 'message'

* `Buffer` `data`

Emitted when the web socket has received data. The data will be contained in
the `data` Buffer argument.

### Event: 'ping'

* `Buffer` `data`

Emitted when the socket has received a ping. The ping's payload is contained in
the `data` argument.

### Event: 'pong'

* `Buffer` `data`

Emitted when the socket has received a pong. The pong's payload is contained in
the `data` argument.

### WebSocketConnection.send

`void send(Buffer data, Boolean mask)`

Send data to the other end of the web socket connection. The `data` parameter
should contain the data payload to send. The `mask` parameter says whether the
data payload should be masked.

### WebSocketConnection.ping

`void ping(Buffer data, Boolean mask)`

Send a ping to the other end of the web socket connection. The `data` parameter
should contain the data payload to send. The `mask` parameter says whether the
data payload should be masked.

### WebSocketConnection.pong

`void pong(Buffer data, Boolean mask)`

Send a pong to the other end of the web socket connection. The `data` parameter
should contain the data payload to send. The `mask` parameter says whether the
data payload should be masked.

Sample Apps
-----------
* [Web Socket Server sample](../samples/websockets/WebSocketServer.js)
* [Node Web Socket Client sample](../samples/websockets/NodeWebSocketClient.js)