Skip to content

Commit

Permalink
Merge pull request #161 from aorcajo/master
Browse files Browse the repository at this point in the history
Connect callback
  • Loading branch information
jrief committed Feb 2, 2016
2 parents d13876f + e30a5ce commit ad65df6
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
5 changes: 5 additions & 0 deletions docs/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,18 @@ and access the Websocket code:
var ws4redis = WS4Redis({
uri: '{{ WEBSOCKET_URI }}foobar?subscribe-broadcast&publish-broadcast&echo',
receive_message: receiveMessage,
connected: on_connected,
heartbeat_msg: {{ WS4REDIS_HEARTBEAT }}
});
// attach this function to an event handler on your site
function sendMessage() {
ws4redis.send_message('A message');
}
function on_connected() {
ws4redis.send_message('Hello');
}
// receive a message though the websocket from the server
function receiveMessage(msg) {
Expand Down
5 changes: 4 additions & 1 deletion ws4redis/static/js/ws4redis.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ function WS4Redis(options, $) {
missed_heartbeats = 0;
heartbeat_interval = setInterval(send_heartbeat, 5000);
}
if ($.type(opts.connected) === 'function') {
opts.connected();
}
}

function on_close(evt) {
Expand All @@ -74,7 +77,7 @@ function WS4Redis(options, $) {
if (opts.heartbeat_msg && evt.data === opts.heartbeat_msg) {
// reset the counter for missed heartbeats
missed_heartbeats = 0;
} else if (typeof opts.receive_message === 'function') {
} else if ($.type(opts.receive_message) === 'function') {
return opts.receive_message(evt.data);
}
}
Expand Down

0 comments on commit ad65df6

Please sign in to comment.