-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
122 lines (102 loc) · 3.96 KB
/
server.js
File metadata and controls
122 lines (102 loc) · 3.96 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
'use strict';
const express = require("express");
const {createServer} = require("http");
const {Server} = require("socket.io");
const app = express();
const Sentry = require('@sentry/node');
const Tracing = require("@sentry/tracing");
const ent = require('ent');
const httpServer = createServer(app);
const io = new Server(httpServer, {
cors: {
origins: [
'http://localhost:4200',
'https://www.secrethouse.online'
]
}
});
Sentry.init({
dsn: "https://ac5eefb263564a459ed5fb089aceecd2@o1064146.ingest.sentry.io/6511891",
integrations: [
// enable HTTP calls tracing
new Sentry.Integrations.Http({ tracing: true }),
// enable Express.js middleware tracing
new Tracing.Integrations.Express({ app }),
],
// Set tracesSampleRate to 1.0 to capture 100%
// of transactions for performance monitoring.
// We recommend adjusting this value in production
tracesSampleRate: 1.0,
});
// RequestHandler creates a separate execution context using domains, so that every
// transaction/span/breadcrumb is attached to its own Hub instance
app.use(Sentry.Handlers.requestHandler());
// TracingHandler creates a trace for every incoming request
app.use(Sentry.Handlers.tracingHandler());
app.set('view engine', 'ejs');
const users = [];
app.get('/', (req, res) => {
res.render('index');
})
app.get('/client', function (req, res) {
res.sendFile(__dirname + "/client.js")
})
app.get("/debug-sentry", function mainHandler(req, res) {
throw new Error("My first Sentry error!");
});
io.on("connection", (socket) => {
// Private message
socket.on("private message", (partyId, roomId, username, avatar, message, fromUserId, toUserId, type) => {
socket.broadcast.emit(toUserId, {partyId, username, avatar, message, fromUserId, toUserId, type});
});
socket.on('disconnect', function () {
users.forEach(function (user) {
if (user.client === socket.client.id) {
socket.broadcast.emit('message_' + user.partyId + '_' + user.roomId, {message: user.username + ' is disconnected '});
for (let i=0; i < users.length; i++) {
if (users[i] === user) {
users.splice(i, 1);
break;
}
}
}
})
});
// TODO: add RoomList to send event in every room. (Foreach)
socket.on('event', function (partyId, roomId, username, avatar, message, fromUserId, toUserId) {
let type = 'event';
message = ent.encode(message);
users.forEach(function (user) {
if (user.fromUserId === fromUserId && roomId !== user.roomId) {
socket.broadcast.emit('message_' + user.partyId + '_' + user.roomId, {
partyId: user.partyId,
roomId: user.roomId,
username,
avatar,
message: user.username + ' vient de quitter la pièce',
fromUserId,
toUserId,
type
});
}
});
users.push({username, avatar, fromUserId, partyId, roomId});
socket.broadcast.emit('message_' + partyId + '_' + roomId,{partyId, roomId, username, avatar, message, fromUserId, toUserId, type});
});
// Dès qu'on reçoit un message, on récupère le pseudo de son auteur et on le transmet aux autres personnes
socket.on('tab-general', function (partyId, roomId, username, avatar, message, fromUserId, toUserId) {
users.push({username, avatar, fromUserId, partyId, roomId});
console.log(partyId, roomId, username, avatar, message, fromUserId, toUserId);
socket.broadcast.emit('message_' + partyId + '_' + roomId,{partyId, roomId, username, avatar, message, fromUserId, toUserId, type: 'general-message'});
});
});
// The error handler must be before any other error middleware and after all controllers
app.use(Sentry.Handlers.errorHandler());
// Optional fallthrough error handler
app.use(function onError(err, req, res, next) {
// The error id is attached to `res.sentry` to be returned
// and optionally displayed to the user for support.
res.statusCode = 500;
res.end(res.sentry + "\n");
});
httpServer.listen(3000);