Receiving "Error: InvalidStateError: DOM Exception 11" on HTML5 websocket when calling .send(data) using Node.js -


i trying develop small chat application using node.js. first node project getting error , i'm not sure how solve it.

i have written simple chat server in node below.

var port = 3000; var net = require('net'); var clients = [];  var server = net.createserver(function (socket) {   clients.push(socket);   bindeventhandlers(socket); });  setinterval(function(){   console.log(clients.length); }, 2000);  server.listen(port); console.log('server listening on localhost:' + port);  function bindeventhandlers(socket) {   socket.on('data', function(data){     broadcastdatatoallclient(data, socket);   });   socket.on('close', function(){     clients.splice(clients.indexof(socket), 1);   });   socket.on('error', function(){     console.log('known error << "it\'s feature!"');   }) }  function broadcastdatatoallclient(data, socket) {   (var client in clients) {       if(clients[client] == socket){ continue; }       clients[client].write(data);   } } 

and quick client interface

<!doctype html> <html>     <head>         <title>node.js chat server</title>         <script type="text/javascript">             function init(){                 if (window.websocket) {                     var input = document.getelementbyid('messagebox');                     var websocket = new websocket("ws://localhost:3000/");                     input.onkeyup = function(e){                         if(e.keycode == 13) {                             console.log('sending message');                             websocket.send('some data');                         }                     };                     websocket.onopen = function(e){                         alert('open');                     }                     websocket.onmessage = function(e){                         console.log('message');                     }                     websocket.onclose = function(e){                         console.log('close');                     }                     websocket.onerror = function(e){                         console.log('error');                     }                    } else {                     console.log('unable support :(');                 }             }         </script>     </head>     <body lang="en" onload="init();">         <h1>node.js chat server</h1>         <h3>welcome simple chat server!</h3>         <input id="messagebox" size="50" />     </body> </html> 

when call websocket.send('some data') receive error error: invalidstateerror: dom exception 11. in chat-server code have loop logs number of current clients know browser connected.

not sure go here , appreciated.

alex

when websocket.send('some data') socket connection must established, or socket.readystate == 1.

you can check websocket events here. make sure never happens, should send after connection open, using socket.onopen event handler. can this.

if(websocket.readystate == 1){     websocket.send('some data');   } else{     websocket.onopen = function(e){         websocket.send('some data');     } } 

Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -