-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.js
42 lines (34 loc) · 1.08 KB
/
client.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
var app = require('express')();
var express = require('express');
var http = require('http').Server(app);
var path = require('path');
var io = require('socket.io')(http);
app.use(express.static(path.join(__dirname,"public")));
app.get('/', function(req, res) {
res.send("ok");
// res.sendFile(path.join(__dirname,"public","index.html"));
});
// users = [];
const account = [];
io.on('connection', function(socket){
console.log("a user connected");
socket.on('setusername',function(data)
{
console.log(data);
// users.push(data);
socket.broadcast.emit('broadcast',{username: data + ' joined the chat'});
socket.emit('userset',{username: data});
account[socket.id] = data;
});
socket.on('msg',function(data){
socket.broadcast.emit('newmsg',data);
});
socket.on('disconnect', function(data)
{
console.log('user disconnected!');
socket.broadcast.emit('left',{name: account[socket.id]});
});
});
http.listen(3000, function() {
console.log('listening on *:3000');
});