1.0.3 • Published 1 year ago
iiws v1.0.3
iiws
A Websocket Server frame for node that is cleaner, easier, more essential and better for learning.
Get started
const http = require("http");
const WSS = require('iiws');
const httpServer = http.createServer();
httpServer.listen(500);
const ws = new WSS(httpServer);
ws.on("connect", (cli)=> {
console.log("a client connected")
ws.broadcast("Hello! every client!"); // broadcast to all online client
cli.send("Welcome, a user"); // just send message to one user
cli.ping(); // send ping message
console.log(ws.clients); // [cli1, cli2, cli3...] get all online clients array
cli.on("message", (data)=> {
console.log(data); // you can get messages from the browser
});
cli.on("close", ()=> {
console.log("a client closed");
});
cli.on("error", (err)=> {
console.log(err.message);
});
});
Websocket basic rules
Handshakes
Frame format
opcode
opcode | conception |
---|---|
0x0 | means a fragment |
0x1 | means a TEXT frame |
0x2 | a BINARY frame |
0x3->7 | reserved code |
0x8 | means disconnecting |
0x9 | means a ping operation |
0xA | means a pong operation |
0xB->F | reserved code |
How to build a websocket server with native nodejs
There is detailed explaination, it's friendly to learn the theory of WebSocket.
See ws.js