Friday 21 September 2018

ws library is not sending or receiving messages.

So I am creating a simple WebSocket server that takes messages sent by one client and broadcasts it to all clients. The code is below.const WebSocket = require("ws"); const ws = new WebSocket.Server({port: 8081}); ws.on("message", message => { console.log("message"); ws.send(message);}); setInterval(()=>{ws.send("test");}, 1000); All clients connect successfully. However, when I send a message, the function in ws.on("message") does not get called. "message" does not appear in my terminal. The last line was added just to test sending, but when ws.send("test") gets called, I get TypeError: ws.send is not a function. The API docs for ws tell me that this will work, so what is going on here?

Submitted September 21, 2018 at 05:48PM by Windows-Sucks

No comments:

Post a Comment