mirror of
https://github.com/abrendan/MicDropMessages.git
synced 2025-06-16 12:45:01 +02:00
47 lines
1.1 KiB
JavaScript
47 lines
1.1 KiB
JavaScript
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 = [];
|
|
|
|
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.");
|
|
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);
|
|
})
|
|
|
|
socket.on("leave", function(room, username){
|
|
if (room && username) {
|
|
socket.leave(room);
|
|
io.in(room).emit("recieve", "Server : " + username + " has left the chat.");
|
|
}
|
|
});
|
|
})
|