{"action":"created","issue":{"url":"https://api.github.com/repos/websockets/ws/issues/337","labels_url":"https://api.github.com/repos/websockets/ws/issues/337/labels{/name}","comments_url":"https://api.github.com/repos/websockets/ws/issues/337/comments","events_url":"https://api.github.com/repos/websockets/ws/issues/337/events","html_url":"https://github.com/websockets/ws/issues/337","id":38827029,"number":337,"title":"Constantly crashing when sending. Error not opened","user":{"login":"thailemon","id":7326906,"avatar_url":"https://avatars.githubusercontent.com/u/7326906?v=3","gravatar_id":"","url":"https://api.github.com/users/thailemon","html_url":"https://github.com/thailemon","followers_url":"https://api.github.com/users/thailemon/followers","following_url":"https://api.github.com/users/thailemon/following{/other_user}","gists_url":"https://api.github.com/users/thailemon/gists{/gist_id}","starred_url":"https://api.github.com/users/thailemon/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/thailemon/subscriptions","organizations_url":"https://api.github.com/users/thailemon/orgs","repos_url":"https://api.github.com/users/thailemon/repos","events_url":"https://api.github.com/users/thailemon/events{/privacy}","received_events_url":"https://api.github.com/users/thailemon/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":5,"created_at":"2014-07-26T21:50:14Z","updated_at":"2015-04-24T21:05:34Z","closed_at":null,"body":"I'm suspecting it's because client is trying to send to a dead socket.\r\nAny other way to handle this?\r\n\r\nMy scenario is mobile App -> browser. App gets disconnect event too late, so it keeps sending packets while the browser is already disconnected.\r\n\r\nHere's my test code:\r\n\r\n    var Client = [];\r\n    function findClient(url) {\r\n    return (Client.indexOf(url));\r\n    }\r\n\r\n\r\n    wss.on('connection', function(ws) {\r\n        Client.push(ws);\r\n        console.log('Connected: %s', ws.upgradeReq.url);\r\n        console.log('Clients: %s', Client.length);\r\n\r\n    ws.on('message', function(message, flags) {\r\n\r\n        for( var i = 0; i < Client.length; i++) {\r\n             Client[i].send(message, function(error) {\r\n             if(error != null) {\r\n             console.log('error: %s', error);\r\n             ws.close();\r\n             ws._socket.destroy();\r\n             Client.splice(findClient(ws.upgradeReq.url));\r\n    }\r\n    });\r\n\r\n    ws.on('close', function(user) {\r\n        console.log('Disconnected: %s', ws.upgradeReq.url);\r\n        ws.close();\r\n        ws._socket.destroy();\r\n        Client.splice(findClient(ws.upgradeReq.url));\r\n        console.log('Clients: %s', Client.length);\r\n\r\n\r\n\r\n      });\r\n           ws.on('error', function(error) {\r\n            if(error != null) {\r\n             console.log('error: %s', error);\r\n             ws.close();\r\n             ws._socket.destroy();\r\n             Client.splice(findClient(ws.upgradeReq.url));\r\n        }\r\n      });\r\n\r\n        ws.on('close', function(user) {\r\n        console.log('Disconnected: %s', ws.upgradeReq.url);\r\n        ws.close();\r\n        ws._socket.destroy();\r\n        Client.splice(findClient(ws.upgradeReq.url));\r\n        console.log('Clients: %s', Client.length);\r\n\r\n      });\r\n\r\n\r\nAny help appreciated :)\r\n"},"comment":{"url":"https://api.github.com/repos/websockets/ws/issues/comments/96065110","html_url":"https://github.com/websockets/ws/issues/337#issuecomment-96065110","issue_url":"https://api.github.com/repos/websockets/ws/issues/337","id":96065110,"user":{"login":"galambalazs","id":309987,"avatar_url":"https://avatars.githubusercontent.com/u/309987?v=3","gravatar_id":"","url":"https://api.github.com/users/galambalazs","html_url":"https://github.com/galambalazs","followers_url":"https://api.github.com/users/galambalazs/followers","following_url":"https://api.github.com/users/galambalazs/following{/other_user}","gists_url":"https://api.github.com/users/galambalazs/gists{/gist_id}","starred_url":"https://api.github.com/users/galambalazs/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/galambalazs/subscriptions","organizations_url":"https://api.github.com/users/galambalazs/orgs","repos_url":"https://api.github.com/users/galambalazs/repos","events_url":"https://api.github.com/users/galambalazs/events{/privacy}","received_events_url":"https://api.github.com/users/galambalazs/received_events","type":"User","site_admin":false},"created_at":"2015-04-24T21:05:34Z","updated_at":"2015-04-24T21:05:34Z","body":"I've seen this problem come up for about 99% of the people starting to use this library. \r\n\r\nInteresting to note that the WHATWG spec only talks about throwing an error when the socket is in CONNECTING state (otherwise the send simply has no effect). \r\n\r\nhttps://html.spec.whatwg.org/multipage/comms.html#dom-websocket-send\r\n\r\n> The send(data) method transmits data using the connection. If the readyState attribute is CONNECTING, it must throw an InvalidStateError exception. Otherwise, the user agent must run the appropriate set of steps from the following list: [...]\r\n\r\n"}}