{"action":"created","issue":{"url":"https://api.github.com/repos/kanaka/noVNC/issues/538","labels_url":"https://api.github.com/repos/kanaka/noVNC/issues/538/labels{/name}","comments_url":"https://api.github.com/repos/kanaka/noVNC/issues/538/comments","events_url":"https://api.github.com/repos/kanaka/noVNC/issues/538/events","html_url":"https://github.com/kanaka/noVNC/issues/538","id":109788684,"number":538,"title":"WebSocket not closed after some errors, RFB.connect() never ends.","user":{"login":"Augustin-SysFera","id":2370810,"avatar_url":"https://avatars.githubusercontent.com/u/2370810?v=3","gravatar_id":"","url":"https://api.github.com/users/Augustin-SysFera","html_url":"https://github.com/Augustin-SysFera","followers_url":"https://api.github.com/users/Augustin-SysFera/followers","following_url":"https://api.github.com/users/Augustin-SysFera/following{/other_user}","gists_url":"https://api.github.com/users/Augustin-SysFera/gists{/gist_id}","starred_url":"https://api.github.com/users/Augustin-SysFera/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Augustin-SysFera/subscriptions","organizations_url":"https://api.github.com/users/Augustin-SysFera/orgs","repos_url":"https://api.github.com/users/Augustin-SysFera/repos","events_url":"https://api.github.com/users/Augustin-SysFera/events{/privacy}","received_events_url":"https://api.github.com/users/Augustin-SysFera/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/kanaka/noVNC/labels/bug","name":"bug","color":"e62929"},{"url":"https://api.github.com/repos/kanaka/noVNC/labels/question","name":"question","color":"DDDDDD"}],"state":"open","locked":false,"assignee":{"login":"DirectXMan12","id":119737,"avatar_url":"https://avatars.githubusercontent.com/u/119737?v=3","gravatar_id":"","url":"https://api.github.com/users/DirectXMan12","html_url":"https://github.com/DirectXMan12","followers_url":"https://api.github.com/users/DirectXMan12/followers","following_url":"https://api.github.com/users/DirectXMan12/following{/other_user}","gists_url":"https://api.github.com/users/DirectXMan12/gists{/gist_id}","starred_url":"https://api.github.com/users/DirectXMan12/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/DirectXMan12/subscriptions","organizations_url":"https://api.github.com/users/DirectXMan12/orgs","repos_url":"https://api.github.com/users/DirectXMan12/repos","events_url":"https://api.github.com/users/DirectXMan12/events{/privacy}","received_events_url":"https://api.github.com/users/DirectXMan12/received_events","type":"User","site_admin":false},"milestone":null,"comments":1,"created_at":"2015-10-05T12:47:21Z","updated_at":"2015-10-13T14:15:41Z","closed_at":null,"body":"Hi all,\r\n\r\nin our app, we sometimes try to connect to a WebSocket before everything is set-up correctly (VNC server started, websockify launched, etc.). To make this transparent for the user, we have written a custom updateStatus method: when the state is \"failed\", we increment a counter and retry, until we reach a certain number of retries, at which point we stop retrying.\r\n\r\nThis is how it looks:\r\n\r\n```\r\nfunction updateState(rfb, state, oldstate, msg) {\r\n    /* some code */\r\n    switch (state) {\r\n        case 'failed':\r\n            level = \"error\";\r\n            setTimeout(\r\n                function() {\r\n                    if (retries < maxRetries) {\r\n                        retries++;\r\n                        sendConnect();\r\n                    }\r\n                }\r\n                , 2000);\r\n            break;\r\n    /* more code */\r\n}\r\n\r\n/* all variables are initialized somewhere */\r\nfunction sendConnect() {\r\n    $.getJSON(\r\n            getName,\r\n            function(data) {\r\n                path += \"/?name=\" + data.name;\r\n                rfb.connect(url, port, password, path);\r\n            }\r\n    );\r\n}\r\n```\r\n\r\nNow the problem: when the page is open, we launch sendConnect(). Let's assume the websockify is not started: connection fails (in the JS console, the following two messages are seen in close succession: \"WebSocket on-error event\", \"WebSocket on-close event\"). updateState fires normally, and sendConnect is called a second time, as expected. Only this time, it hangs there and nothing further happens. In the JS console, only one message is seen: \"WebSocket on-error event\".\r\n\r\nIt seems the WebSocket is never closed, so connect never returns, so our custom sendConnect never does.\r\n\r\nI have attempted to fix this by modifying rfb.js, but I have little idea what I am doing, and it is likely not a good modification:\r\n```\r\nOriginal: (line 214 of rfb.js)\r\nthis._sock.on('error', function (e) {\r\n    Util.Warn(\"WebSocket on-error event\");\r\n});\r\n\r\nModified:\r\nthis._sock.on('error', function (e) {\r\n    Util.Warn(\"WebSocket on-error event\");\r\n    this._fail(\"Got unexpected WebSocket connection\");\r\n}.bind(this));\r\n```\r\n\r\nWhen using this modification, our logic works as expected.\r\n\r\nCould anyone please help me understand what's going on, and how we could improve our code to fix the problem?\r\n\r\nThanks a bunch!"},"comment":{"url":"https://api.github.com/repos/kanaka/noVNC/issues/comments/147727530","html_url":"https://github.com/kanaka/noVNC/issues/538#issuecomment-147727530","issue_url":"https://api.github.com/repos/kanaka/noVNC/issues/538","id":147727530,"user":{"login":"Augustin-SysFera","id":2370810,"avatar_url":"https://avatars.githubusercontent.com/u/2370810?v=3","gravatar_id":"","url":"https://api.github.com/users/Augustin-SysFera","html_url":"https://github.com/Augustin-SysFera","followers_url":"https://api.github.com/users/Augustin-SysFera/followers","following_url":"https://api.github.com/users/Augustin-SysFera/following{/other_user}","gists_url":"https://api.github.com/users/Augustin-SysFera/gists{/gist_id}","starred_url":"https://api.github.com/users/Augustin-SysFera/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Augustin-SysFera/subscriptions","organizations_url":"https://api.github.com/users/Augustin-SysFera/orgs","repos_url":"https://api.github.com/users/Augustin-SysFera/repos","events_url":"https://api.github.com/users/Augustin-SysFera/events{/privacy}","received_events_url":"https://api.github.com/users/Augustin-SysFera/received_events","type":"User","site_admin":false},"created_at":"2015-10-13T14:15:41Z","updated_at":"2015-10-13T14:15:41Z","body":"Just a quick update: after some tests on our side, our modification seems to be working in our use cases.\r\n\r\nI am still very much unsure of the further consequences of this."}}