Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,40 @@ angular.module('MyCoolChainedWebApp', ['ngWebsocket'])
});
});
```
Want to send a binary message? Set the 'binary' parameter to true.

```javascript
'use strict';

angular.module('MyCoolChainedWebApp', ['ngWebsocket'])
.run(function ($websocket) {
var ws = $websocket.$new('ws://localhost:12345')
.$on('$open', function () {
console.log('Oh my gosh, websocket is really open! Fukken awesome!');

var data = new Uint8Array([21,31]);

ws.binaryType = 'arraybuffer';

ws.$emit('ping', 'hi listening websocket server') // send a message to the websocket server
.$emit('pong', data, true);
})
.$on('pong', function (data) {
console.log('The websocket server has sent the following data:');

if (data instanceof ArrayBuffer) {
console.log('A binary message of length', data.toArrayBuffer().byteLength;
} else {
console.log(data);
}

ws.$close();
})
.$on('$close', function () {
console.log('Noooooooooou, I want to have more fun with ngWebsocket, damn it!');
});
});
```

Your back-end team is lazy? No problem: we can do it on our own!

Expand Down
32 changes: 23 additions & 9 deletions ng-websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,20 +214,34 @@
return me;
};

me.$$send = function (message) {
if (me.$ready()) me.$$ws.send(JSON.stringify(message));
else if (me.$$config.enqueue) me.$$queue.push(message);
me.$$send = function (message, binary) {
if (me.$ready()) {
if (binary === undefined || !binary) {
me.$$ws.send(JSON.stringify(message));
} else {
me.$$ws.send(message);
}
}
else if (me.$$config.enqueue) {
me.$$queue.push(message);
}
};

me.$emit = function (event, data) {
me.$emit = function (event, data, binary) {
if (typeof event !== 'string') throw new Error('$emit needs two parameter: a String and a Object or a String');

var message = {
event: event,
data: data
};
if (binary === undefined || !binary) {

var message = {
event: event,
data: data
};

me.$$send(message);
me.$$send(message);
} else {
// do not compose JSON message, send 'data' as-is
me.$$send(data, binary);
}

return me;
};
Expand Down