Answer the question
In order to leave comments, you need to log in
How to fix error on nodejs with websocket?
I'm making a chat, with the ability to store history on the server. Connected the 'ws' module to NodeJS.
When I load the chat page, NodeJs crashes with an
error
var WebSocketServer = new require('ws');
var webSocketServer = new WebSocketServer.Server({
port: 8080
});
// connected users
var clients = {};
// all messages
var history = [];
webSocketServer.on('connection', function(socket) {
var id = Math.random();
clients[id] = socket;
clients[id].send(history); //ошибка видимо здесь
socket.on('message', function(obj) {
history.push(obj);
for (var key in clients) {
clients[key].send(obj);
}
});
socket.on('close', function() {
delete clients[id];
});
});
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question