wilk / ng-websocket Goto Github PK
View Code? Open in Web Editor NEWAngularJS HTML5 WebSocket powerful library
License: MIT License
AngularJS HTML5 WebSocket powerful library
License: MIT License
The current module makes working directly with the raw data coming from the socket tricky. I would like to add an option to not automatically JSON encode/decode the data being sent and retrieved. This would be defaulted to enabling the parsing, for backwards compatibility.
Threre is any way to update the URL during websocket reconection process? I basically need it because we provide a token in the URL during conection process, and this specific token is updated during application lifecycle, consequently we need to refresh it in the original URL
Last commit was like 2 years ago
Thanks for your work on ng-websocket
. I have been using ng-websocket
for one of my project, It gets the work done but there are couple of issues me and others are facing, like....
$destroy
methodI will be happy to submit some PR to solve those issues if maintainer is willing to maintain the project actively.
cc @wilk
In case if network is changed, then it doesn't recognize the network change and keep trying to emit the data, which never reach to server. Is there any kind of ping/pong process supported to confirm the connection.
It would be great if you could update bower.json dependencies for Angular 1.3.0.
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.
How does this project compare to https://github.com/AngularClass/angular-websocket?
Bower keeps asking me which version to use of angular because the version in the bower file is lower than my current version of angular. Could I post a PR?
Hi,
That's a good documentation. But can you also detail the browser (both desktop & mobile) support and fallback mechanism support (if not there already, any plans to add fallbacks) for older browsers.
Thanks.
Hello, thanks for this great library.
The example shows the socket being instantiated in .run
I am trying to move this to a factory - it works fine, but when I switch to background and back, it creates a new instance of ws (because I am calling the factory method on resume). Is there any way to make sure only one instance of ws is used?
the onclose event does not pass the event object to $close event.
the event object is needed to determine the reason.
This is a great module, but it doesn't support supplying the protocols array second argument to WebSocket.
Hello, may anybody help with this error please ?
WebSocket connection to 'ws://localhost:12345/' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED
Sometimes I have a socket that is specific to a "page" in a single page application. The user might not ever return to that page so when they leave I'd rather just close the socket and re-open it if they happen to return.
At the moment I have to do something like this since the websockets are stored globally:
var ws = $websocket.$new({url: wsURL, protocols:[]});
$scope.$on("$destroy", function handler() {
ws.$close();
delete $websocket.$$websocketList[ws.$$config.url];
});
It seems like there should just be a $destroy method in addition to $close so you can always safely call $new without weird stuff happening.
Hello, thanks for the excellent library.
I have the following question: I have implemented a form of authentication using this library - unless specific connection parameters are provided in the web socket URL the server rejects the connection.
This calls the on close event. The on close event is also called when there is a network disconnect or the server goes down. Is there any way to implement a differentiation between a server reject and a network loss? I know I can implement a solution where a server sends a message before disconnecting, but I am trying to avoid a bi-directional messaging system for now.
thanks
How do I setup a custom event?
I'm trying to do it as given in $on. I have the following in my controller to catch a config
event I'm firing from a node ws server.
var socket = $websocket.$new('ws://localhost:3000')
.$on('$open', function () {
console.log('Server connected');
})
.$on('$close', function () {
console.log('Server disconnected');
})
.$on('$error', function () {
console.log('Server error');
})
.$on('$message', function (message) {
console.log(message);
})
.$on('config', function (data) {
console.log(data);
});
I'm logging all messages to make sure I receive the config object. The js console prints Object {config: 'testconfig'}
so I'm receiving it. but I can't get the .$on('config', function (data) {console.log(data);});
bit to activate.
What am I missing? Is this a bug with $on or is that not how its supposed to be used? Should I be taking this to ws?
My server code for reference
var WebSocketServer = require('ws').Server;
var socketServer = new WebSocketServer({port: 3000});
socketServer.on('connection', function(socket) {
console.log('server connected');
socket.on('close', function() {
console.log('server disconnected');
})
.send(JSON.stringify({config:'testconfig'}))
});
When initially creating .$new to open the WS, on another controller, if you do .$get, and listen to socket, it sends the messages twice to the socket, one from the new and one from the get instance.
I think this is a bug, im going to look into to see if i can fix it, any ideas will be helpful.
Thanks
WebSocket connection to 'ws://apilocal.ws.com:8887/' failed: Error during WebSocket handshake: Sent non-empty 'Sec-WebSocket-Protocol' header but no response was received
Has anyone had this issue?
Hi,
When i try to communicate between two browser. I open a socket in one but i can't get it in the second one. $get method return to me undefined, the url is good. It's like the second browser doesn't see the first one socket.
My code :
var socketCtrl = this;
socketCtrl.socket = [];
socketCtrl.newSocket = function (nameSocket) {
var ws = $websocket.$new({
url: 'ws://localhost',
reconnect: true, // it will reconnect after 2 seconds
mock: true
});
ws.$on('message', function (message) {
console.log(message); // it prints 'a parrot response'
});
ws.$on('$open', function () {
console.log('Connection open!');
});
ws.$on('$close', function () {
console.log('Connection closed!');
});
socketCtrl.socket.push(ws);
};
socketCtrl.getSocket = function (nameSocket) {
var ws = $websocket.$get('ws://localhost');
console.log(ws.$ready());
socketCtrl.socket.push(ws);
};
socketCtrl.closeSocket = function (idSocket) {
if (socketCtrl.socket.length > idSocket) {
socketCtrl.socket[idSocket].$close();
}
};
socketCtrl.sendMessage = function (idSocket, message) {
if (socketCtrl.socket.length > idSocket) {
socketCtrl.socket[idSocket].$emit('message', message);
}
};
Thanks
Normally when using the $http service I'd use the withCredentials: true option in the config object.
Is it possible to use ng-websocket in a way that is consistent with how I use $http, i.e. always using credentials?
When trying to open the websocket and automatic reconnect is enabled, an exponential number of retries are taking place.
I believe this is due no checking if a close event is already part of a retry or not.
Clearing the reconnect interval-task before scheduling one fixes the issue: line 169
if (me.$$config.reconnect) {
if (me.$$reconnectTask) {
clearInterval(me.$$reconnectTask);
delete me.$$reconnectTask;
}
Hi
I have a websocket connection open and data is being sent from the server. However I cannot apply this data to the controller $scope?
angular.module('bedappApp').controller('MainCtrl', function ($scope, $websocket) {
var ws = $websocket.$new({
url: 'ws://localhost:8080'
});
$scope.message = '';
ws.$on('$open', function () {
ws.$emit('hello', 'world'); // it sends the event 'hello' with data 'world'
});
ws.$on('$message', function (message) {
console.log(message);
$scope.message = message;
});
ws.$on('$close', function () {
console.log('Got close, damn you silly wifi!');
});
$scope.$watch('message', function (change) {
console.log('hey, myVar has changed!', change);
});
});
The watch never updates, also a simple two way binding on the html page does not update.
The console does show the data being received and printed.
Object {0: "1.308500E-01", type: "Potential", key: "0", value: "1.308500E-01", source: "temp"} scripts.a3a83e70.js:1
Object {type: "board", key: "AmbientTemperature", value: "2.238280E+01", AmbientTemperature: "2.238280E+01", source: "temp"} scripts.a3a83e70.js:1
Object {0: "2.453350E+01", type: "Temperature", key: "0", value: "2.453350E+01", source: "temp"} scripts.a3a83e70.js:1
Object {0: "8.703000E-02", type: "Potential", key: "0", value: "8.703000E-02", source: "temp"} scripts.a3a83e70.js:1
Object {type: "board", key: "AmbientTemperature", value: "2.233590E+01", AmbientTemperature: "2.233590E+01", source: "temp"} scripts.a3a83e70.js:1
Object {0: "2.351650E+01", type: "Temperature", key: "0", value: "2.351650E+01", source: "temp"} scripts.a3a83e70.js:1
Object {0: "4.774000E-02", type: "Potential", key: "0", value: "4.774000E-02", source: "temp"}
I am at a loss as to what is going on ? isolate scope ?
Regards
Gary
How do I sent headers with my connection string?
I need to send something like:
Header: Authorization
Value: Bearer SOME_JWT_TOKEN
var ws = $websocket('wss://' + websocket, {
reconnectIfNotNormalClose: true
});
var ws = $websocket.$new('ws://localhost:12345');
ws.$on('$open', function () {
ws.$emit('ping', 'hi listening websocket server'); // send a message to the websocket server
}
this is the example given with the project !
but I want to know how should I send a message to "ws://localhost:12345/abc/ac" ?
as the example above ,$websocket.$new('ws://localhost:12345');
it just locate the host and port and not locate the context "/abc/ac" ,
where should I put the context?
and how should I send the messge ??
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.