-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
39 lines (32 loc) · 941 Bytes
/
index.js
File metadata and controls
39 lines (32 loc) · 941 Bytes
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
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var sock = null;
var Server = function(browserPort) {
this.io = require('socket.io')(server); //Creates my http server
app.use(express.static(__dirname));
server.listen(browserPort, function () {
console.log('Server listening at port %d', browserPort);
});
}
Server.prototype.handle_connection = function(socket){
sock = socket;
console.log(sock.id);
}
Server.prototype.init = function(){
console.log("Server initialized!")
this.io.on('connection', this.handle_connection)
}
Server.prototype.get_time = function(id, msg) {
return new Date().toString();
}
Server.prototype.sendClientMsg = function(id, msg) {
if(sock) {
sock.emit(id, {payload:msg});
}
}
server = new Server("8080");
server.init();
setInterval(function() {
server.sendClientMsg('message', 'Hello from server: ' + server.get_time());
}, 1000);