{"action":"reopened","issue":{"url":"https://api.github.com/repos/alexhultman/node-lws/issues/45","repository_url":"https://api.github.com/repos/alexhultman/node-lws","labels_url":"https://api.github.com/repos/alexhultman/node-lws/issues/45/labels{/name}","comments_url":"https://api.github.com/repos/alexhultman/node-lws/issues/45/comments","events_url":"https://api.github.com/repos/alexhultman/node-lws/issues/45/events","html_url":"https://github.com/alexhultman/node-lws/issues/45","id":142167627,"number":45,"title":"Stored sockets cannot be used to send or be closed","user":{"login":"LordMajestros","id":10727946,"avatar_url":"https://avatars.githubusercontent.com/u/10727946?v=3","gravatar_id":"","url":"https://api.github.com/users/LordMajestros","html_url":"https://github.com/LordMajestros","followers_url":"https://api.github.com/users/LordMajestros/followers","following_url":"https://api.github.com/users/LordMajestros/following{/other_user}","gists_url":"https://api.github.com/users/LordMajestros/gists{/gist_id}","starred_url":"https://api.github.com/users/LordMajestros/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/LordMajestros/subscriptions","organizations_url":"https://api.github.com/users/LordMajestros/orgs","repos_url":"https://api.github.com/users/LordMajestros/repos","events_url":"https://api.github.com/users/LordMajestros/events{/privacy}","received_events_url":"https://api.github.com/users/LordMajestros/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":3,"created_at":"2016-03-20T14:14:30Z","updated_at":"2016-03-20T21:18:21Z","closed_at":null,"body":"I was playing around with node-lws, trying to implement one connection per browser using cookies.\r\nConnections without cookies or with invalid cookies are rejected, this works well.\r\nWhen a user connects I store his socket in a javascript object called cookieRooms with the cookie as the key.\r\nIf he connects again from another tab/window I allow the new tab/window connection but close the socket from the previous tab/window using the socket stored in cookieRooms. This does not work.\r\nIt seems the socket objects are invalid outside the callbacks.\r\nHere is my code\r\n```\r\nvar lws = require('lws');\r\nvar server = new lws.Server({ port: 3000, bufferSize: 2048, maxMessageSize: 1024 * 3  });\r\nvar connections = 0;\r\n\r\nvar cookieRooms = {};\r\nvar userRooms ={};\r\n\r\nserver.on('error', function (error) {\r\n    console.log('Could not start server!');\r\n});\r\n\r\nserver.on('upgrade', function (socket, headers) {\r\n    console.log('Upgrading to WebSocket!');\r\n    if(getPricedropSessionId(headers,socket)){\r\n        //server.handleUpgrade(socket, request);\r\n        console.log('Upgraded!!');\r\n        return false;\r\n    }\r\n    else{\r\n        console.log(\"Didn't pass muster, closed.\");\r\n        server.close(socket);\r\n    }\r\n    return true;\r\n});\r\n\r\nserver.on('connection', function (socket) {\r\n    connections++;\r\n    if (connections % 1000 == 0 || connections < 1000) {\r\n        console.log('Connections: ' + connections);\r\n    }\r\n    server.send(socket,new Buffer('Hi, your number is '+connections),false);\r\n    var socketCookie = server.getUserData(socket);\r\n    if(socketCookie){\r\n        var pdSID = getPdSidFromSocket(socketCookie);\r\n        if(pdSID){\r\n            console.log('Socket '+pdSID+' connected');\r\n        }\r\n        else{\r\n            server.close(socket)\r\n            console.log('Socket closed, no pdSID.');\r\n        }\r\n    }\r\n    else{\r\n        server.close(socket);\r\n        console.log('Socket closed, no user data.');\r\n    }\r\n});\r\n\r\n// we can echo per message (buffered, slow)\r\nserver.on('message', function (socket, message, binary) {\r\n    console.log('Message: ' + message);    \r\n    server.send(socket, message, false);\r\n    var socketCookie = server.getUserData(socket);\r\n    if(socketCookie){\r\n        var pdSID = getPdSidFromSocket(socketCookie);\r\n        if(pdSID){\r\n            console.log('Socket '+pdSID+' connected');\r\n        }\r\n    }\r\n});\r\n\r\nserver.on('close', function (socket) {\r\n    connections--;\r\n    connections=connections<0?0:connections;\r\n    var socketCookie = server.getUserData(socket);\r\n    if(socketCookie){\r\n        var pdSID=getPdSidFromSocket(server.getUserData(socket));\r\n        var userId=getUserSidFromSocket(server.getUserData(socket));\r\n        if(pdSID){\r\n            if(cookieRooms.hasOwnProperty(pdSID)){\r\n                delete cookieRooms[pdSID];\r\n                console.log('Found and deleted pdsid.');\r\n            }\r\n        }\r\n    }\r\n});\r\n\r\nconsole.log('Running echo server on port 3000');\r\n\r\nvar cookieParser = require('cookie-parser');\r\nvar cookie = require('cookie');\r\n\r\nfunction getPricedropSessionId(headers,socket){\r\n    if (headers.cookie) {\r\n        var pd_cookie = cookie.parse(headers.cookie);\r\n        if(!pd_cookie.hasOwnProperty('pd.sid')){\r\n            console.log('No cookie transmitted');\r\n            return false;\r\n        }\r\n        try{\r\n            var pdSID = cookieParser.signedCookie(pd_cookie['pd.sid'], 'my_secret_key');\r\n            if (pd_cookie['pd.sid'] == pdSID) {\r\n                console.log('Cookie is invalid');\r\n                return false;\r\n            }\r\n            else{\r\n                if(cookieRooms.hasOwnProperty(pd_cookie['pd.sid'])){\r\n                    console.log(\"This guy \"+pd_cookie['pd.sid']+\" already exists\");\r\n                    if(cookieRooms[pd_cookie['pd.sid']]){\r\n                        //This does not work\r\n                        server.send(cookieRooms[pd_cookie['pd.sid']],new Buffer(\"{'m':'You have connected elsewhere and have been disconnected here.'}\"),false);\r\n                        //Neither does this\r\n                        server.close(cookieRooms[pd_cookie['pd.sid']]);\r\n                        delete cookieRooms[pd_cookie['pd.sid']];\r\n                        connections--;\r\n                        connections=connections<0?0:connections;\r\n                        console.log('Found and closed');\r\n                    }\r\n                }\r\n                server.setUserData(socket, '\"pd.sid\":\"'+pd_cookie['pd.sid']+'\"');\r\n                cookieRooms[pd_cookie['pd.sid']]= socket;//server.getFd(socket);\r\n                console.log(\"PDSID: \"+pdSID);\r\n                return true;\r\n            }\r\n        }\r\n        catch(exception){\r\n            console.log('An exception occured');\r\n            console.log(exception);\r\n            return false;\r\n        }\r\n    } \r\n    else {\r\n        console.log('No cookie transmitted');\r\n        return false;\r\n    }\r\n    console.log('Unusual case');\r\n    return false;\r\n}\r\nfunction getPdSidFromSocket(socketData){\r\n    try{\r\n        var result=JSON.parse('{'+socketData+'}');\r\n        if(result && result.hasOwnProperty('pd.sid')){\r\n            return result['pd.sid'];\r\n        }\r\n    }\r\n    catch(exception){\r\n        console.log(exception);\r\n    }\r\n    return null;\r\n}\r\n```\r\nThe server side code is pretty simple, after connecting to the http server on another port, I run this in a google chrome or firefox console\r\n```\r\nvar connection = new WebSocket('ws://localhost:3000');\r\n// When the connection is open, send some data to the server\r\nconnection.onopen = function () {\r\n  connection.send('Ping'); // Send the message 'Ping' to the server\r\n};\r\n\r\n// Log errors\r\nconnection.onerror = function (error) {\r\n  console.log('WebSocket Error ');\r\n  console.log(error);\r\n};\r\nconnection.onclose=function(){\r\n  console.log('Connection closed');\r\n}\r\n// Log messages from the server\r\nconnection.onmessage = function (e) {\r\n  console.log('Server: ' + e.data);\r\n};\r\n```\r\nThe onclose event is never called.\r\nI tested this on a Macbook pro(Retina 15 inch late 2013), OS X El Capitan 10.11.2\r\nIs there a way to save a socket and use it later to send or close the socket outside a callback?"}}