Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
AngularJS+WebSocket
//WebSocket
.factory('WebsocketService', ['$rootScope', '$timeout', function ($rootScope, $timeout) {
var _ws;
var _username = '';
var messages = [];
var users = [];
function onMessage(e) {
var data = JSON.parse(decodeURIComponent(e.data));
$rootScope.$apply(function () {
if (data.type === 'users') {
users = data.message;
$rootScope.$broadcast('websocket', 'users', users);
return;
}
messages.splice(0, 0, {user: data.user, message: data.message, date: data.date});
$rootScope.$broadcast('websocket', 'message', messages);
});
}
return {
login: function (url, username) {
_username = username;
_ws = new WebSocket(url);
_ws.onmessage = onMessage;
_ws.onopen = function () {
_ws.send(encodeURIComponent(JSON.stringify({type: 'change', message: _username})));
};
},
logoff: function () {
_ws.close();
_ws = null;
_username = '';
users = [];
$rootScope.$broadcast('websocket', 'users', users);
},
send: function (message) {
_ws.send(encodeURIComponent(JSON.stringify({type: 'message', message: message})));
//
_ws.onmessage = function (message) {
console.log("_ws.onmessage:", message);
}
}
};
}])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.