Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // if user is running mozilla then use it's built-in WebSocket
- window.WebSocket = window.WebSocket || window.MozWebSocket;
- var connection = new WebSocket('wss://localhost:443', 'echo-protocol');
- connection.onopen = function () {
- // connection is opened and ready to use
- console.log('client opened socket');
- $scope.send();
- };
- connection.onerror = function (error) {
- // an error occurred when sending/receiving data
- console.log('err: '+error);
- };
- connection.onmessage = function (message) {
- // try to decode json (I assume that each message
- // from server is json)
- console.log('server response:'+message);
- try {
- var json = JSON.parse(message.data);
- console.log(json);
- } catch (e) {
- console.log('This doesn\'t look like a valid JSON: ',
- message.data);
- return;
- }
- // handle incoming message
- };
- $scope.send = function() {
- // Tell the server this is client 1 (swap for client 2 of course)
- connection.send(JSON.stringify({
- id: "client1"
- }));
- };
- });
Add Comment
Please, Sign In to add comment