forked from MrSwitch/peer.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
118 lines (93 loc) · 2.19 KB
/
app.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
var fs = require('fs');
var path = require('path');
var port=process.env.PORT || 5000;
var http=require('http');
var app=http.createServer(function(req,res){
// res.write("server listening to port:"+port);
// res.end();
// return;
var filePath = '.' + req.url;
if (filePath == './')
filePath = './index.html';
console.log('request starting: ' + filePath);
var extname = path.extname(filePath);
var contentType = 'text/html';
switch (extname) {
case '.js':
contentType = 'text/javascript';
break;
case '.css':
contentType = 'text/css';
break;
case '.png':
contentType = 'image/png';
break;
}
path.exists(filePath, function(exists) {
if (exists) {
fs.readFile(filePath, function(error, content) {
if (error) {
res.writeHead(500);
res.end();
return;
}
else {
res.writeHead(200, { 'Content-Type': contentType });
res.end(content, 'utf-8');
return;
}
});
}
else {
res.writeHead(404);
res.end();
return;
}
});
//res.end();
}).listen(port);
console.log(port);
var socket=require("socket.io");
var io=socket.listen(app);
// This has to be run on port 80
io.configure(function (){
io.set("transports", ["xhr-polling"]);
io.set("polling duration", 10);
});
// Create a new Client
io.sockets.on('connection', function (socket) {
// Tell the client that the connection succeeded
socket.emit('ready');
// Broadcast to yourself that you have entered the room
socket.send(JSON.stringify({
type : 'sessionConnected',
from : socket.id
}));
socket.broadcast.send(JSON.stringify({
type : 'connectionCreated',
from : socket.id
}));
console.log('New connection');
// Listen to events
socket.on('message', function(data){
console.log('Message Recieved');
data = JSON.parse(data);
data.from = socket.id;
console.log(data);
// How do we handle this message?
// Does the message contain a 'to' field?
if(data.to){
io.sockets.socket(data.to).send(JSON.stringify(data));
}
else{
socket.broadcast.send(JSON.stringify(data));
}
});
/*
socket.on('disconnected', function(){
socket.broadcast.send(JSON.stringify({
type : 'connectionDestroyed',
id : socket.id
}));
}); */
});