-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebsocket.js
More file actions
33 lines (33 loc) · 1.37 KB
/
websocket.js
File metadata and controls
33 lines (33 loc) · 1.37 KB
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
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var webSLib = require("ws");
// WebSocket, { WebSocketServer}
var wss = new webSLib.WebSocketServer({ port: 6666 });
console.log("WS listening on port ".concat(wss.address().port), wss.address());
var webSockets = [];
wss.on('connection', function (ws) {
console.log("User connected");
webSockets[webSockets.length] = ws;
// console.log(ws)
ws.on('message', function (data, isBinary) {
console.log("WS:// - Recieved: ".concat(data));
// console.log(wss.clients)
// for single response
ws.send(JSON.stringify({ transmission_data: JSON.parse(data.toString()), recieved: true }), { binary: false });
// for broadcast
// wss.clients.forEach((c : WebSocket) => {
// if(c.readyState == WebSocket.OPEN){
// c.send(JSON.stringify({transmission_data: JSON.parse(data.toString()), recieved: true}), {binary: false})
// }
// });
});
ws.on('error', function (data) { return console.log("Error: ".concat(data)); });
ws.on('close', function (code, reason) { return console.log("Closing - code: ".concat(code, "\nreason:"), reason.toJSON()); });
ws.send('{"connected": true}');
});
wss.on('error', function (ws) {
console.error('WEBSOCKET ERROR');
});
wss.on("close", function () {
});
exports.default = wss;