-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
32 lines (23 loc) · 821 Bytes
/
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
var express = require('express');
var socketio = require('socket.io');
var port = 1337;
var server = express.createServer();
var io = socketio.listen(server);
server.get('/', function (req, res) {
res.sendfile(__dirname + '/public/index.html');
});
io.sockets.on('connection', function(client) {
client.emit('getUsername');
client.on('message', function(msg) {
client.send("<b>You</b>: "+msg);
client.broadcast.send("<b>"+client.username+"</b>: "+msg);
});
client.on('setUsername', function(username) {
client.username = username;
client.broadcast.send("<b>"+username+" ist dem Chat beigetreten.</b>");
});
client.on('disconnect', function() {
client.broadcast.send("<b>"+client.username+" hat den Chat verlassen.</b>");
});
});
server.listen(port);