-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
42 lines (32 loc) · 1.02 KB
/
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
36
37
38
39
40
41
42
const http = require("http");
const express = require("express");
const socketio = require("socket.io");
const path = require("path");
const app = express();
const httpserver = http.Server(app);
const io = socketio(httpserver);
const gamedirectory = path.join(__dirname, "html");
app.use(express.static(gamedirectory));
httpserver.listen(3000);
var rooms = [];
var usernames = [];
var today = new Date();
var time = today.getSeconds();
io.on('connection', function(socket){
socket.on("join", function(room, username){
if (username != ""){
rooms[socket.id] = room;
usernames[socket.id] = username;
socket.leaveAll();
socket.join(room);
io.in(room).emit("recieve", "Server : " + username + " has entered the chat. " + time + " seconds ago ");
socket.emit("join", room);
}
})
socket.on("send", function(message){
io.in(rooms[socket.id]).emit("recieve", usernames[socket.id] +" : " + message);
})
socket.on("recieve", function(message){
socket.emit("recieve", message);
})
})