Skip to content

Instantly share code, notes, and snippets.

@ellismarte ellismarte/chat.js Secret
Last active Oct 25, 2016

Embed
What would you like to do?
export default class ChatController {
/**
* In order to set up the chat feature we need to complete a number of tasks
* in a particular order. In order to achieve this we call the initialize
* method which is a generator and has a #next method.
*/
constructor($scope, $http, datastore, chateventhandler){
'ngInject';
this.scope = $scope;
this.http = $http;
this.directory = 'http://192.168.99.100:3010'
this.chatEventHandler = chateventhandler;
this.connected = false;
this.config = {"withCredentials": true, "Access-Control-Allow-Origin": true};
this.host = null;
this.ws = null;
this.account = datastore.retrieve('account');
this.username = datastore.retrieve('username');
this.signature = datastore.retrieve('signature');
this.initializer = this.initialize();
this.initializer.next();
this.initializer.next();
this.initializer.next();
this.initializer.next();
}
*initialize(){
yield this.authorize();
yield this.makeConnection();
yield this.setConnected();
yield this.setUpEventHandlers();
}
authorize(){
return new Promise((resolve, reject) => {
var body = JSON.stringify({
account: this.account,
username: this.username,
signature: this.signature
})
this.http.post(this.directory + '/authorize', body, this.config)
.then(response => {
if (response['status'] == 200){
this.host = response['host'];
resolve();
}
else {
reject();
}
})
})
}
makeConnection(){
return new Promise((resolve, reject) => {
var ws = new WebSocket(this.host + '/connect');
if (ws) {
this.ws = ws;
resolve(ws);
}
else {
console.log('connection wasnt made', response)
reject();
}
})
}
setUpEventHandlers(){
this.chatEventHandler['ws'] = this.ws
this.chatEventHandler['signature'] = this.signature
this.ws.onopen = this.chatEventHandler['onopen'];
this.ws.onmessage = this.chatEventHandler['onmessage'];
this.ws.onerror = this.chatEventHandler['onerror'];
this.ws.onclose = this.chatEventHandler['onclose'];
}
setConnected(){
if (this.ws.readyState == 1){
this.connected = true
return
}
this.connected = false
}
reconnect(){
}
}
chat-ctrl.js:26Uncaught ReferenceError: regeneratorRuntime is not defined(anonymous function)
@ chat-ctrl.js:2656 @ chat-ctrl.js:88s @ _prelude.js:1(anonymous function)
@ _prelude.js:181../controllers/analytics/analytics-ctrl @ index.module.js:10s @ _prelude.js:1e
@ _prelude.js:1(anonymous function) @ _prelude.js:1
angular.js:68Uncaught Error: [$injector:modulerr] Failed to instantiate module app due to:
Error: [$injector:nomod] Module 'app' is not available! You either misspelled the module name or forgot to load it.
If registering a module ensure that you specify the dependencies as the second argument.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.