Skip to content

WebSocket wrapper for using single websocket connection with multi events

Notifications You must be signed in to change notification settings

hagino3000/websocket_messaging

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 

Repository files navigation

WebSocket wrapper to handle multi events.

WebSocket sends next format data (JSON)

{
  "eventName" : "imageUrl",
  "data" : {
    "lastRetreiveTime" : "1171815102"
  },
  "pid" : 0,
  "socketKey" : "8GzBJq42m9"
}

pid is sequencial number in this socket. socketKey is a random string using to identify this socket.

An expected format of receive data

{
  "eventName" : "imageUrl",
  "data" : {[
     ....
  ]},
  "pid" : 0,
  "socketKey" : "8GzBJq42m9"
}

Usage

var ws = new WSMessage({
  url:"localhost:8080",
  autoRecovery : true,
  listeners : {
    open : function(){console.info('onopen!!')},
    close : function(){console.info('onclose!!')}
  }
});

// wait push event from server
ws.on('newUser', function(data) {
  console.dir(data);
});

// data request and reveive
ws.on('imageUrl', handleImageUrl); 
ws.send('imageUrl', {lastRetrieveTime : 0000000000});

function handleImageUrl(data) {
 doSomething(data);
}

// unfollow event
ws.un('imageUrl', handleImageUrl);

// close
ws.close();

License

(The MIT License)

Copyright © 2009 Takashi Nishibayashi(hagino3000)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

About

WebSocket wrapper for using single websocket connection with multi events

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published