-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
35 lines (28 loc) · 793 Bytes
/
index.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
var app = require("express")();
var http = require("http").Server(app);
var io = require("socket.io")(http);
app.get("/", function(req, res){
res.sendFile(__dirname + "/index.html");
})
io.on("connection", function(socket){
var username;
socket.on("user connected", function(name){
username = name;
console.log(name + " connected.");
io.emit("user connected", username);
});
socket.on("chat message", function(msg){
console.log("message: " + msg);
io.emit("chat message", msg);
});
socket.on("user typing", function(){
io.emit("user typing", username);
});
socket.on("disconnect", function(socket){
console.log(username + " disconnected.");
io.emit("user disconnected", username);
});
});
http.listen(3000, function(){
console.log("Listening on " + 3000);
});