How to use the on function from ws

Find comprehensive JavaScript ws.on code examples handpicked from public code repositorys.

ws.on is a method in the ws library for Node.js that attaches a listener function to a specific WebSocket event.

285
286
287
288
289
290
291
292
293
294
});

function onSocketConnect(ws) {
  clients.add(ws);

  ws.on('message', function(message) {
    message = message.slice(0, 50); // максимальный размер сообщения 50

    for(let client of clients) {
      client.send(message);
fork icon878
star icon0
watch icon47

+ 7 other calls in file

68
69
70
71
72
73
74
75
76
77

that.wss.on("connection", function (ws, req) {
  const url = new URL(req.url, exampleBaseUrl);
  const reUrlStr = uUrl.rearrangeUrl(url);

  ws.on("close", () => {
    if (Object.keys(that.clientHolders).includes(reUrlStr)) {
      let index = that.clientHolders[reUrlStr].wss.indexOf(ws);

      if (index === -1) {
fork icon0
star icon0
watch icon1

How does ws.on work?

The ws.on function in the WebSocket package is used to attach an event listener to a WebSocket instance, allowing the user to respond to various events emitted by the WebSocket. When an event is emitted, the corresponding callback function registered with the on method is called, with any relevant data passed as arguments.

58
59
60
61
62
63
64
65
66
67
68
69
});


wss.on("connection", (ws) => {
    console.log("connected!");


    ws.on("message", (msg) => {
        let data = JSON.parse(msg);
        if (!Object.keys(games).includes(data.code)) {
            games[data.code] = {
                fen: "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1",
fork icon0
star icon0
watch icon2

11
12
13
14
15
16
17
18
19
20
    client.send(message);
  });
};

wss.on('connection', (ws, req) => {
  ws.on("message", function (data) {
    wss.broadcast(data.toString());
});

// user connection
fork icon0
star icon0
watch icon0

+ 15 other calls in file

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
const WebSocket = require("ws");

const ws = new WebSocket("wss://echo.websocket.org/");

ws.on("open", function () {
  console.log("WebSocket connected");

  // send a message to the server
  ws.send("Hello, server!");
});

ws.on("message", function (data) {
  console.log("Received message from server:", data);
});

ws.on("close", function () {
  console.log("WebSocket disconnected");
});

In this example, ws.on is used to define event listeners for different WebSocket events: 'open' is emitted when the connection is established, 'message' is emitted when a message is received from the server, and 'close' is emitted when the connection is closed. When these events are emitted, the corresponding callback functions are executed.

94
95
96
97
98
99
100
101
102
103
wss.on('connection', function (ws) {
    let location = url.parse(ws.upgradeReq.url, true);
    console.log('[WebSocketServer] connection: ' + location.href);
    ws.on('message', onMessage);
    ws.on('close', onClose);
    ws.on('error', onError);
    if (location.pathname !== '/ws/chat') {
        // close ws:
        ws.close(4000, 'Invalid URL');
    }
fork icon0
star icon0
watch icon0

+ 2 other calls in file

19
20
21
22
23
24
25
26
27
28
function handleError(){
    console.log('WebSocket error');
}
function handleConnection(ws){
    console.log('WebSocket connection');
    ws.on('message',handleMessage);
}
function handleMessage(msg){
    console.log('WebSocket message');
    console.log(msg);
fork icon0
star icon0
watch icon0

31
32
33
34
35
36
37
38
39
40
    return;
}
playerCount++;
ws.send(JSON.stringify({ tag: tags["JOINED"], version }));

ws.on('message', message => {
    receiver(ws, JSON.parse(message));
});

ws.on('close', code => {
fork icon0
star icon0
watch icon1

+ 7 other calls in file