-
Notifications
You must be signed in to change notification settings - Fork 0
/
professor.js
74 lines (63 loc) · 1.78 KB
/
professor.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/**
* Created by Ignacio on 4/8/16.
*/
var io = require('socket.io-client');
var constants = require("./constants");
var prompt = require('prompt');
var User = require("./user");
prompt.start();
prompt.get(['mail'], function (err, result) {
if (err || !result.mail) { return User.prototype.onErr(); }
this.mail = result.mail;
setupConnection()
});
function setupConnection() {
this.description = 'professor';
this.socket = io.connect('http://localhost:'+constants.PORT, {reconnect: true});
User.prototype.handshake(this.socket, this.description, this.mail);
User.prototype.listeners(this.socket);
setupListeners()
}
function setupListeners() {
login();
receivedQuestion();
receivedNotification()
}
function login() {
socket.on(constants.RESULT, function (error) {
if (error){
console.log(error);
process.exit();
}else{
console.log('success!!!!\r\n');
getQuestion()
}
});
}
function getQuestion(){
console.log('asking....')
this.socket.emit(constants.GET_QUESTION);
}
function receivedNotification() {
socket.on(constants.WRITING, function (msg, from) {
console.log('[Professor] professor with mail ' + from + ' writting questionID: ' + msg)
});
}
function receivedQuestion(){
socket.on(constants.RECEIVED_QUESTION, function (question) {
if (question){
answer(question)
}else{
setTimeout(function () {
getQuestion()
},5000);
}
});
}
function answer(question) {
socket.emit(constants.WRITING, question.id, this.mail);
setTimeout(function() {
socket.emit(constants.SEND_ANSWER, 'question of ID:'+question.id);
getQuestion()
}, 1000 + Math.random()*4000);
}