Advertisement
codeplanner

XSockets EventController JavaScript

Apr 6th, 2012
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var ws = null;
  2. var wsEventController = null;
  3. $(function () {
  4.     ws = new jXSockets.xWebSocket("ws://127.0.0.1:4502/GenericText", jXSockets.WEBSOCKET);
  5.     wsEventController = new jXSockets.xWebSocket("ws://127.0.0.1:4502/EventController", jXSockets.WEBSOCKET);
  6.  
  7.     //Bind open/close (open will trigger attachEvents)
  8.     bindSocketEvents();
  9.  
  10.     //On click... get info about the events being listened to...
  11.     $("#pubsub").click(function () {
  12.         //get guid listening to OnChatMessage in controller GenericText
  13.         wsEventController.trigger("GetSubscriptions", { Alias: "GenericText", Event: "OnChatMessage" });
  14.         //get ALL subscriptions
  15.         wsEventController.trigger("GetAllSubscriptions", {});
  16.     });
  17.  
  18.     $("#subscribe").click(function () {
  19.         //Tell the eventcontroller that we have a new client listening to OnChatMessage on GenericText.
  20.         //This will be done in the bind callback in the next version of the JSAPI.
  21.         wsEventController.trigger("Subscribe", { Event: "OnChatMessage", Alias: "GenericText" });
  22.  
  23.         //Tell the eventcontroller that we have a new client listening to OnChatMessage on GenericText.
  24.         //This will be done in the bind callback in the next version of the JSAPI.
  25.         wsEventController.trigger("Subscribe", { Event: "OnFakeMessage", Alias: "GenericText" });
  26.     });
  27.  
  28.     $("#unsubscribe").click(function () {
  29.         ws.unbind("OnChatMessage", function () {
  30.             wsEventController.trigger("Unsubscribe", { Alias: "GenericText", Event: "OnChatMessage" });
  31.         });
  32.  
  33.         ws.unbind("OnFakeMessage", function () {
  34.             wsEventController.trigger("Unsubscribe", { Alias: "GenericText", Event: "OnFakeMessage" });
  35.         });
  36.     });
  37. });
  38.  
  39. // Deal with our WebSockets Events
  40. function attachEvents() {
  41.     //fake subscription for OnchatMessage on controller GenericText
  42.     ws.bind("OnChatMessage", function (d) {
  43.         console.log(d);
  44.     });
  45.  
  46.     //fake subscription for OnFakeMessage on controller GenericText
  47.     ws.bind("OnFakeMessage", function (d) {
  48.         console.log(d);
  49.     });
  50.  
  51.     //Just bind getSubscriptions and console log
  52.     wsEventController.bind("OnGetSubscriptions", function (d) {
  53.         console.log(d);
  54.     });
  55.     //Just bind getAllSubscriptions and console log
  56.     wsEventController.bind("OnGetAllSubscriptions", function (d) {
  57.         console.log(d);                
  58.     });            
  59. }      
  60.  
  61. function bindSocketEvents() {
  62.     ws.bind("close", function (msg) {
  63.         $('#infoDiv').text("No xSocketConnection :(");
  64.     });
  65.  
  66.     ws.bind("open", function () {
  67.         console.log('open');                
  68.         attachEvents();
  69.     });            
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement