forked from gabbygreat/chat-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
87 lines (74 loc) · 2.58 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
// create application/json parser
var jsonParser = bodyParser.json()
// create application/x-www-form-urlencoded parser
var urlencodedParser = bodyParser.urlencoded({ extended: false })
const http = require('http');
const { connect } = require('http2');
const server = http.createServer(app);
const { Server } = require("socket.io");
const io = new Server(server);
// const messages = []
// io.on('connection', (socket) => {
// const username = socket.handshake.query.username
// socket.on('message', (data) => {
// const message = {
// message: data.message,
// senderUsername: username,
// sentAt: Date.now()
// }
// messages.push(message)
// io.emit('message', message)
// })
// });
io.on('connection', (socket) => {
console.log('Connected')
socket.on('message', (data) => {
console.log('sent')
io.emit('message', data);
})
});
app.get('/', function (request, response) {
console.log('loading');
response.send({ 'Hello World': 'ME' })
})
app.get('/users', function (request, response) {
const users = [];
for (let [id, socket] of io.of("/").sockets) {
users.push({
userID: id,
username: socket.username,
});
}
response.send({ 'users': users })
})
app.post('/initiate', urlencodedParser, function (request, response) {
var receipientSocketId = request.query.receipientSocketId;
console.log(request.query.senderName);
io.to(receipientSocketId).emit('request', {
'senderDeviceId': request.query.senderDeviceId,
'receipientSocketId': request.query.receipientSocketId,
'messageId': request.query.messageId,
'senderName': request.query.senderName === "" ? null : request.query.senderName,
'senderSocketId': request.query.senderSocketId,
})
response.send(request.body);
})
app.post('/accept', urlencodedParser, function (request, response) {
console.log('git');
var receipientSocketId = request.query.receipientSocketId;
console.log(receipientSocketId);
io.to(receipientSocketId).emit('accept', {
'senderDeviceId': request.query.senderDeviceId,
'receipientSocketId': request.query.receipientSocketId,
'messageId': request.query.messageId,
'senderName': request.query.senderName === "" ? null : request.query.senderName,
'senderSocketId': request.query.senderSocketId,
})
response.send(request.body);
})
server.listen(3000, () => {
console.log('SERVER: LISTENING');
});