From 7c505bf6ffcf83809212308771e10dec5046aec0 Mon Sep 17 00:00:00 2001 From: Christophe Date: Tue, 9 Sep 2014 17:15:27 +0400 Subject: [PATCH] added onopen event handler --- README.md | 18 +++++++++++++++++- example/js/brain-socket.min.js | 2 +- lib/BrainSocket.js | 4 ++++ lib/brain-socket.min.js | 2 +- 4 files changed, 23 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 6442626..0422e15 100644 --- a/README.md +++ b/README.md @@ -57,7 +57,23 @@ To fire an event to the WebSocket server you can call: app.BrainSocket.message('some.event',[some:data]); ``` -Typically you would fire these messages after user interactions take place in your app. +**Note:** Firing an event before a connection to the websockets server will cause an error. As such, if you want to fire an event as soon as possible after pageload you should use the "onopen" event handler as shown below. + + +```javascript +app.BrainSocket.onopen(function() +{ + // this executes as soon as the connection is established. + app.BrainSocket.message('newClient.event', + { + 'message':'register', + 'user_id':fake_user_id + } + ); +}); +``` + +Typically though, you would fire these messages after user interactions take place in your app. Here's an example with jquery: diff --git a/example/js/brain-socket.min.js b/example/js/brain-socket.min.js index ef1cd08..5f69fdf 100644 --- a/example/js/brain-socket.min.js +++ b/example/js/brain-socket.min.js @@ -1 +1 @@ -function BrainSocketPubSub(){this.subscriptions=[];this.forget=function(e){for(x=0;x0){for(var r=0;r0){for(var r=0;r0){for(var r=0;r0){for(var r=0;r