Advertisement
Guest User

Untitled

a guest
Jan 20th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.19 KB | None | 0 0
  1. var client = {}
  2. //client.clientId = undefined
  3.  
  4.  
  5. client.init = function() {
  6. // this is required to so that the ui is able to communicate with the client through the API specified on the assignment sheet
  7. ui.setClient(client)
  8.  
  9. // create Websocket object
  10. client.socket = new WebSocket('ws://' + document.location.hostname + ':' + config.server.wsport);
  11.  
  12. // on open
  13. client.socket.onopen = function() {
  14. console.log('Connection established!');
  15. };
  16.  
  17. // on close
  18. client.socket.onclose = function() {
  19. ui.fail('Lost connection to the server!');
  20. };
  21.  
  22. // on error
  23. client.socket.onerror = function(event) {
  24. console.log('Error happened: ' + event.data);
  25. };
  26.  
  27. // on message
  28. client.socket.onmessage = function(event) {
  29. var msg = JSON.parse(event.data);
  30.  
  31. switch (msg.type) {
  32. case config.msgtype.CHANNEL_MSG:
  33. ui.addChannelMessage(msg.payload.src, msg.payload.channel, msg.payload.body);
  34. break;
  35. case config.msgtype.CHANNEL_SUB:
  36. ui.addChannelView(msg.payload.channel);
  37. break;
  38. case config.msgtype.CHANNEL_UNSUB:
  39. ui.removeChannelView(msg.payload.channel);
  40. break;
  41. case config.msgtype.CHANNEL_LIST:
  42. ui.updateChannelList(msg.payload.channel_list);
  43. break;
  44. case config.msgtype.QUEUE_PULL:
  45. getPullResponse(msg.payload);
  46. break;
  47. case config.msgtype.ERROR:
  48. ui.fail(msg.payload.error);
  49. break;
  50. }
  51. };
  52. }
  53. client.requestCreateChannel = function(target) {
  54. //logging to the console in a browser is in most cases not a good idea for production code
  55. //here it is just done for making debugging efforts slightly easier in the browser
  56. console.log('client.requestCreateChannel: ' + target)
  57.  
  58. // for now, the following is invoked without considering any communication with the server
  59. // it is probably a bad idea to readily create a new tab in the UI at this time...
  60. // ... consider what happens if there are errors or if the server refuses to create this channel for some reason
  61. if (target.length != 0) {
  62. client.socket.send(createMsg(config.msgtype.CHANNEL_CREATE, createChannelPayload(target)));
  63. }
  64. else {
  65. ui.fail("No channel name entered")
  66. }
  67. }
  68.  
  69. client.requestUnsubscribe = function(dst) {
  70. console.log('client.requestUnsubscribe: ' + dst)
  71. client.socket.send(createMsg(config.msgtype.CHANNEL_UNSUB, createChannelPayload(dst)));
  72. }
  73.  
  74. client.requestSubscribe = function(dst) {
  75. console.log('client.requestSubscribe: ' + dst)
  76. client.socket.send(createMsg(config.msgtype.CHANNEL_SUB, createChannelPayload(dst)));
  77. }
  78.  
  79. client.requestPublish = function(dst, msg) {
  80. console.log('client.requestPublish: ' + dst + ' ' + msg)
  81. var payload = createChannelPayload(dst);
  82. payload.body = msg;
  83. client.socket.send(createMsg(config.msgtype.CHANNEL_MSG, payload));
  84. }
  85.  
  86. function createMsg(type, payload) {
  87. var msg = {};
  88. msg.type = type;
  89. msg.payload = payload;
  90. return JSON.stringify(msg);
  91. }
  92.  
  93. client.requestPull = function(dst) {
  94. console.log('client.requestPull: ' + dst)
  95. client.socket.send(createMsg(config.msgtype.QUEUE_PULL, createQueuePayload(dst)));
  96. }
  97.  
  98. client.requestPush = function (target, msg) {
  99. console.log('client.requestPushChannel: ' + target)
  100. if (target.length != 0) {
  101. var temp = createQueuePayload(target);
  102. temp.msg = msg;
  103. client.socket.send(createMsg(config.msgtype.QUEUE_PUSH,temp ));
  104. }
  105. else {
  106. ui.fail("No channel name entered")
  107. }
  108. }
  109.  
  110.  
  111. function getPullResponse(payload) {
  112. if(payload.error == 1)
  113. {
  114. console.log('There is nothing to pull');
  115. }
  116. else
  117. {
  118. console.log('Element pulled');
  119. //for futher use
  120. var msg = {};
  121. msg = payload.msg;
  122. }
  123. }
  124.  
  125. function createChannelPayload(channel) {
  126. var payload = {};
  127. payload.channel = channel;
  128. return payload;
  129. }
  130.  
  131. function createQueuePayload(dst) {
  132. var payload = {};
  133. payload.dst = dst;
  134. return payload;
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement