-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
60 lines (50 loc) · 1.69 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
const express_ = require('express');
const app =express_();
const http = require('http').Server(app);
const io = require('socket.io')(http);
const urlRegex=/(((https?:\/\/)|(www\.))[^\s]+)/g;
app.use(express_.static(__dirname+'/public'))
app.get('/',(req,res)=>{
res.sendFile(__dirname + '/entry.html');
});
io.on('connection',(socket)=>{
console.log('user connected');
socket.on('disconnect',()=>{
console.log("disconnect");
});
socket.on('user joined',(props)=>{
io.emit('user joined',props);
});
socket.on("chat-message",(msg,isurl)=>{
const as_url=msg.match(urlRegex,msg);
const url=`<a href="${as_url}" style="color:'blue'">${as_url}</a>`;
msg=msg.replace(as_url,url);
console.log("**************new message**************\n")
console.log(msg);
console.log("\n*********************************************")
io.emit("chat-message",msg);
})
socket.on('create-room',(id)=>{
room = 10;
socket.to(room).emit("newmessage",'hello from room !')
app.get('/room/:id',(req,res)=>{
console.log(req.params.id);
res.sendFile(__dirname+'/index.html')
})
})
socket.on('join-room',(id)=>{
room = id;
app.get('/joinroom/:props',(req,res)=>{
console.log(req.params.props);
res.sendFile(__dirname+'/index.html')
})
socket.join(id);
socket.on("newmessage",()=>{
io.to(room).emit("newmessage","hello this from room")
})
})
})
const PORT = process.env.PORT || 3000
http.listen(PORT,()=>{
console.log("started server at ",PORT);
})