forked from tutsplus/nodejs-websockets-chat-service
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.js
47 lines (33 loc) · 1.03 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
//branch for trying chatnode.js setup
var appPort = process.env.PORT || 3000;
var express = require('express'), app = express();
var http = require('http')
, server = http.createServer(app)
, io = require('socket.io').listen(server);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.set("view options", { layout: false });
app.configure(function() {
app.use(express.static(__dirname + '/public'));
});
app.get('/', function(req, res){
res.render('home.jade');
});
server.listen(appPort);
// app.listen(appPort);
io.configure(function () {
io.set("transports", ["xhr-polling"]);
io.set("polling duration", 10);
});
io.sockets.on('connection', function (socket) {
socket.on('setPseudo', function (data) {
socket.set('pseudo', data);
});
socket.on('message', function (message) {
socket.get('pseudo', function (error, name) {
var data = { 'message' : message, pseudo : name };
socket.broadcast.emit('message', data);
console.log("user " + name + " send this : " + message);
})
});
});