Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var client = {}
- //client.clientId = undefined
- client.init = function() {
- // this is required to so that the ui is able to communicate with the client through the API specified on the assignment sheet
- ui.setClient(client)
- // create Websocket object
- client.socket = new WebSocket('ws://' + document.location.hostname + ':' + config.server.wsport);
- // on open
- client.socket.onopen = function() {
- console.log('Connection established!');
- };
- // on close
- client.socket.onclose = function() {
- ui.fail('Lost connection to the server!');
- };
- // on error
- client.socket.onerror = function(event) {
- console.log('Error happened: ' + event.data);
- };
- // on message
- client.socket.onmessage = function(event) {
- var msg = JSON.parse(event.data);
- switch (msg.type) {
- case config.msgtype.CHANNEL_MSG:
- ui.addChannelMessage(msg.payload.src, msg.payload.channel, msg.payload.body);
- break;
- case config.msgtype.CHANNEL_SUB:
- ui.addChannelView(msg.payload.channel);
- break;
- case config.msgtype.CHANNEL_UNSUB:
- ui.removeChannelView(msg.payload.channel);
- break;
- case config.msgtype.CHANNEL_LIST:
- ui.updateChannelList(msg.payload.channel_list);
- break;
- case config.msgtype.QUEUE_PULL:
- getPullResponse(msg.payload);
- break;
- case config.msgtype.ERROR:
- ui.fail(msg.payload.error);
- break;
- }
- };
- }
- client.requestCreateChannel = function(target) {
- //logging to the console in a browser is in most cases not a good idea for production code
- //here it is just done for making debugging efforts slightly easier in the browser
- console.log('client.requestCreateChannel: ' + target)
- // for now, the following is invoked without considering any communication with the server
- // it is probably a bad idea to readily create a new tab in the UI at this time...
- // ... consider what happens if there are errors or if the server refuses to create this channel for some reason
- if (target.length != 0) {
- client.socket.send(createMsg(config.msgtype.CHANNEL_CREATE, createChannelPayload(target)));
- }
- else {
- ui.fail("No channel name entered")
- }
- }
- client.requestUnsubscribe = function(dst) {
- console.log('client.requestUnsubscribe: ' + dst)
- client.socket.send(createMsg(config.msgtype.CHANNEL_UNSUB, createChannelPayload(dst)));
- }
- client.requestSubscribe = function(dst) {
- console.log('client.requestSubscribe: ' + dst)
- client.socket.send(createMsg(config.msgtype.CHANNEL_SUB, createChannelPayload(dst)));
- }
- client.requestPublish = function(dst, msg) {
- console.log('client.requestPublish: ' + dst + ' ' + msg)
- var payload = createChannelPayload(dst);
- payload.body = msg;
- client.socket.send(createMsg(config.msgtype.CHANNEL_MSG, payload));
- }
- function createMsg(type, payload) {
- var msg = {};
- msg.type = type;
- msg.payload = payload;
- return JSON.stringify(msg);
- }
- client.requestPull = function(dst) {
- console.log('client.requestPull: ' + dst)
- client.socket.send(createMsg(config.msgtype.QUEUE_PULL, createQueuePayload(dst)));
- }
- client.requestPush = function (target, msg) {
- console.log('client.requestPushChannel: ' + target)
- if (target.length != 0) {
- var temp = createQueuePayload(target);
- temp.msg = msg;
- client.socket.send(createMsg(config.msgtype.QUEUE_PUSH,temp ));
- }
- else {
- ui.fail("No channel name entered")
- }
- }
- function getPullResponse(payload) {
- if(payload.error == 1)
- {
- console.log('There is nothing to pull');
- }
- else
- {
- console.log('Element pulled');
- //for futher use
- var msg = {};
- msg = payload.msg;
- }
- }
- function createChannelPayload(channel) {
- var payload = {};
- payload.channel = channel;
- return payload;
- }
- function createQueuePayload(dst) {
- var payload = {};
- payload.dst = dst;
- return payload;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement