-
Notifications
You must be signed in to change notification settings - Fork 78
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cannot reopen websocket after calling $close() #8
Comments
Hi @gtarcea! angular.controller('MyCtrl', ['$websocket', function ($websocket) {
var ws = $websocket.$new({
url: 'ws://localhost:12345',
mock: true
});
var count = 0;
ws.$on('$open', function () {
if (count === 0) ws.$close();
count++;
});
ws.$on('$close', function () {
ws.$open();
});
}]); When the ws is opened, it closes, just once. |
Could this be noted in the documentation? After a few reads of the docs, I ended up making the same mistake as the original posted of this bug report. Only finding the bug report clarified things for me... thanks! :-) |
+1 — it's very surprising that |
My fix has been monkeypatching $websocket.$get = function() { return undefined; } Ofc this isn't entirely backwards compatible… but also works around this otherwise remarkable behavior. |
ng-websocket will not allow you to re-open a websocket if you have called $close() on it. After working through the code I've found the problem is in the function $websocketService line 68:
if (typeof ws === 'undefined') {
If the socket was previously opened and then closed, then wss.$get() will return the ws that was closed, so ws is not equal to 'undefined' and it never actually trys to create a new WebSocket. I changed line 68 to:
if (typeof ws === 'undefined' || ws.$status() === ws.$CLOSING || ws.$status() === ws.$CLOSED) {
I can send a pull request if required.
The text was updated successfully, but these errors were encountered: