-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
98 lines (79 loc) · 2.02 KB
/
server.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
var http = require('http'),
url = require('url'),
path = require('path'),
fs = require('fs'),
io = require('socket.io'),
sys = require('sys'),
server = http.createServer(function(request , response){
var uri = url.parse(request.url).pathname;
var filename = path.join(process.cwd(), uri);
path.exists(filename, function(ex){
if(!ex){
response.writeHead(404, {'Content-Type':'text/plain'});
response.end("cant find it ....");
return;
}
fs.readFile(filename, "binary", function(err , data){
if(err){
response.writeHead(500, {'Content-Type': 'text/plain'});
response.write('404');
response.end("nahi mili file");
return;
}
response.writeHead(200);
response.write(data, "binary");
response.end("");
//checking was removed from here
});
});
});
server.listen(8080);
// socket.io, I choose you
//var socket = io.listen(server);
var socket = io.listen(server);
process.openStdin().addListener("data", function(text){
socket.broadcast(text);
});
function login(username, client)
{
var response = {"msgtype" : "login",
"status" : "blue",
"username" : username
};
socket.broadcast(response);
response.status = "green";
client.send(response);
}
function sendtoclients(msg, username)
{
var response = {"msgtype": "chat", "post": msg, "username": username };
socket.broadcast(response);
}
socket.on('connection', function(client){
//console.log(client); //working thing
client.on('message', function(msg){
console.log("msg = " + msg + ",cid =" + client.sessionId);
switch(msg["msgtype"])
{
case "login":
login(msg["username"], client);
break;
case "chat":
sendtoclients(msg["post"], msg["username"]);
break;
}
//if("login" == msg["msgtype"])
//{
// login(msg["username"], client);
//}
//console.log(msg["msgtype"]);
//socket.broadcast(msg);
});
});
/*socket.on('connection', function(client){
client.on('message', function(){
console.log('message from client on socket');
});
client.on('disconnect', function(){});
});
*/