-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsocket.js
More file actions
28 lines (24 loc) · 812 Bytes
/
socket.js
File metadata and controls
28 lines (24 loc) · 812 Bytes
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
const express = require("express");
const http = require("http");
const WebSocket = require("ws");
const server = http.createServer(express);
const socketServer = new WebSocket.Server({ server });
socketServer.on("connection", (ws) => {
ws.on("message", (data) => {
socketServer.clients.forEach((client) => {
if (client !== ws && client.readyState === WebSocket.OPEN) {
client.send(data);
}
});
});
});
server.listen("8080", () => console.log("Chat room live at 8080"));
// socketServer.on("connection", function connection(ws) {
// ws.on("message", function incoming(data) {
// socketServer.clients.forEach(function each(client) {
// if (client !== ws && client.readyState === WebSocket.OPEN) {
// client.send(data);
// }
// });
// });
// });