-
Notifications
You must be signed in to change notification settings - Fork 14
/
index.js
33 lines (26 loc) · 829 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
var express = require('express'); // look for express in file
var socket = require('socket.io');
// App setup
var app = express();
var server = app.listen(9000, function(){
console.log('listening to requests on prt 9000');
});
var messages = [];
// Static files
app.use(express.static('public'));
// Socket setup
var io = socket(server);
io.on('connection', function(socket){
console.log('made socket connection', socket.id);
socket.emit('initial-connection', messages);
// Handle chat event
// When I hear chat message sent to me, fire callback
// function to receive data and pass function
socket.on('chat', function(data){
messages.push(data);
io.sockets.emit('chat', data);
});
socket.on('typing', function(data){
socket.broadcast.emit('typing', data);
});
});