modulews
websocket support module
WebSocketThe protocol is a full-duplex communication protocol based on the TCP protocol. It establishes an uninterrupted connection between the browser and the server, can realize real-time two-way data transmission, and can support data transmission in any format. In fibjs,WebSocketThe support module provides corresponding API interfaces to implementWebSocketServer-side and client-side development.
Instructions:
1var ws = require('ws');
Create a server:
1
2
3
4
5
6
7
8
9var ws = require('ws');
var http = require('http');
var svr = new http.Server(80, {
'/ws': ws.upgrade((conn, req) => {
conn.onmessage = e => console.log(e.data);
})
});
svr.start();
In the above code, the built-in fibjs is first loaded through the require function.WebSocketSupport modules, then usehttp.ServerThe class creates an HTTP server instance, specifies the listening port number and HTTP request path, and then uses the upgrade function under the specified path to upgrade the request toWebSocketconnection, the server automatically creates aWebSocketThe object provides onopen, onmessage, onerror, onclose and other events, and can send data to the client through the send function.
useWebSocketClient:
1
2
3
4var ws = require('ws');
var conn = new ws.Socket('ws://127.0.0.1/ws');
conn.onmessage = e => console.log(e.data);
Can be created bySocketinstance and specify aWebSocketURL to complete the client connection.SocketThe class provides onmessage, onerror, onclose and other callback functions and send functions, which can send data to the server and receive data sent by the server.
have to be aware of is,WebSocketSupport modules are simplyWebSocketAn implementation of the protocol that needs to work on top of the HTTP protocol. On the server side, the upgrade function can be used to convert HTTP requests intoWebSocketconnection, and on the client side, you need to passWebSocketProtocol URL to specify the server address to be connected.
object
Message
Create a websocket message object, seeWebSocketMessage
1WebSocketMessage ws.Message;
Socket
1WebSocket ws.Socket;
static function
upgrade
Create a websocket protocol handler fromhttpReceive upgrade request and handshake, generateWebSocketobject
1static Handler ws.upgrade(Function accept);
Call parameters:
- accept: Function, connection success processing function, the callback will pass two parameters, the first parameter is the receivedWebSocketObject, the second parameter is the handshakeHttpRequestobject
Return results:
- Handler, returns the protocol handler, can be used withHttpServer,Chain,RoutingWaiting for docking```
Create a websocket protocol handler fromhttpReceive upgrade request and handshake, generateWebSocketobject
1
2static Handler ws.upgrade(Object opts,
Function accept);
Call parameters:
- opts: Object, connection option, default is {}
- accept: Function, connection success processing function, the callback will pass two parameters, the first parameter is the receivedWebSocketObject, the second parameter is the handshakeHttpRequestobject
Return results:
- Handler, returns the protocol handler, can be used withHttpServer,Chain,RoutingWaiting for docking
opts contains additional options for the request, supported as follows:
1
2
3
4{
"perMessageDeflate": false, // specify whether to use permessage-deflate, default is true
"maxPayload": 67108864 // specify the maximum allowed message size, default is 64MB
}
constant
CONTINUE
Specify websocket message type 0, which represents a continuation frame
1const ws.CONTINUE = 0;
TEXT
Specify websocket message type 1, which represents a text frame
1const ws.TEXT = 1;
BINARY
Specify websocket message type 2, which represents a binary frame
1const ws.BINARY = 2;
CLOSE
Specify websocket message type 8, the connection is closed
1const ws.CLOSE = 8;
PING
Specify websocket message type 9, which represents a ping frame
1const ws.PING = 9;
PONG
Specify websocket message type 10, which represents a pong frame
1const ws.PONG = 10;
CONNECTING
SpecifyWebSocketstatus, indicating that it is connecting
1const ws.CONNECTING = 0;
OPEN
SpecifyWebSocketstatus, indicating open status
1const ws.OPEN = 1;
CLOSING
SpecifyWebSocketstatus, indicating that the CLOSE message has been sent and is waiting to be closed.
1const ws.CLOSING = 2;
CLOSED
SpecifyWebSocketstatus, indicating that it has been closed
1const ws.CLOSED = 3;