-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
49 lines (39 loc) · 1.29 KB
/
index.js
File metadata and controls
49 lines (39 loc) · 1.29 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
const express = require("express");
const app = express();
const http = require("http").createServer(app);
const io = require("socket.io")(http);
app.use(express.static("public"));
const workspaces = io.of(/^\/\w+$/);
const allSockets = {};
workspaces.on("connection", (socket) => {
const workspace = socket.nsp;
allSockets[socket.id] = socket;
socket.on("broadcast", ({ eventName, data }) => {
socket.broadcast.emit(eventName, data);
});
socket.on("all", ({ eventName, data }) => {
workspace.emit(eventName, data);
});
socket.on("direct", ({ socketId, eventName, data }) => {
const chosenSocket = allSockets[socketId];
if (!chosenSocket) return;
chosenSocket.emit(eventName, data);
});
socket.on("disconnect", () => {
socket.broadcast.emit("connectionDestroyed", { socketId: socket.id });
});
socket.broadcast.emit("connectionCreated", { socketId: socket.id });
});
// https://socket.io/docs/namespaces/
// this middleware will be assigned to each namespace
workspaces.use((socket, next) => {
// Random middleware used for documentation.
// ensure the user has access to the workspace
next();
});
io.on("connection", (socket) => {
console.log("a user connected");
});
http.listen(3005, () => {
console.log("listening on *:3005, realtime.songz.dev");
});