Last active
September 7, 2015 18:04
-
-
Save calvintwr/e0373553637ea6d47a23 to your computer and use it in GitHub Desktop.
Chat
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script> | |
var BASE_URI = 'http://localhost:2999'; | |
var socket = io.connect(BASE_URI); | |
var Chat = { | |
init: function(opts) { | |
var mandatory = ['socket', 'myUserId']; | |
for (var i in mandatory) { | |
if (typeof opts[mandatory[i]] === 'undefined') throw new Error('mandatory options are not defined.'); | |
this[mandatory[i]] = opts[mandatory[i]]; | |
} | |
return this; | |
}, | |
registerAsOnline: function() { | |
var self = this; | |
this.socket.emit('register-as-online', {userId: self.myUserId}, function(isConnected) { | |
if (!isConnected) return _retry(self.socket, self.myUserId); | |
console.log('connection is good.'); | |
}); | |
function _retry(socket, myUserId) { | |
setTimeout(function() { | |
self.registerAsOnline(socket, myUserId) | |
}, 1000); | |
} | |
return this; | |
}, | |
receiveChatNotification: function(callback) { | |
var self = this; | |
this.socket.on('incomingChat', function(data) { | |
if(data.fromUserId !== self._inConversationWith) { | |
if(typeof callback === 'function') callback(data); | |
} | |
}); | |
return this; | |
}, | |
startConversationWith: function(withWho, performWhenChatComesIn) { | |
var self = this; | |
this._inConversationWith = withWho; | |
this.socket.on('incomingChat', function(data) { | |
if(data.fromUserId === self._inConversationWith) { | |
if(typeof performWhenChatComesIn === 'function') performWhenChatComesIn(data); | |
} | |
}); | |
return this; | |
}, | |
endConversation: function() { | |
this._inConversationWith = false; | |
}, | |
sendChat: function(message) { | |
var self = this; | |
var data = { | |
talkToUserId: self._inConversationWith | |
}; | |
if (typeof message !== 'string') { | |
console.log(message); | |
console.log(message.html()) | |
data.text = message.html(); | |
} else { | |
data.text = message; | |
} | |
console.log(data); | |
var ajax = $.ajax({ | |
url: '/api/v1/chat/add/as-traveller', | |
type: 'PUT', | |
data: JSON.stringify(data), | |
cache: false, | |
processData: false, | |
contentType: 'application/json', | |
dataType: 'json' | |
}); | |
ajax.done(function(result) { | |
if (result.success === true) $('#customizeBlock').html('Successfully customized!'); | |
}); | |
ajax.fail(function(err) { | |
alert('failed!'); | |
console.log(err); | |
}); | |
}, | |
socket: undefined, | |
myUserId: undefined, | |
_inConversationWith: false | |
}; | |
var chat = Object.create(Chat); | |
chat.init({ | |
socket: socket, | |
myUserId: 1 | |
}); | |
socket.on('connected', function() { | |
chat | |
.registerAsOnline() | |
.receiveChatNotification(function(data) { | |
console.log(data); | |
alert('bump chat badge.'); | |
}).startConversationWith(2, function(data) { | |
alert('update chat'); | |
console.log(data); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment