@mwilki7/

NodeJS Chatroom

Nodejs

No description

fork
loading
Files
  • index.js
  • index.html
  • package-lock.json

This Plugin Crashed!

Error: Error: must not create an existing file {"type":"CREATE_FILE","wid":"0.0795402064564179","path":"index.js","file":{"path":"index.js","content":{"asEncoding":{"base64":"dmFyIGV4cHJlc3MgPSByZXF1aXJlKCdleHByZXNzJykKICAgICwgYXBwID0gZXhwcmVzcygpCiAgICAsIGh0dHAgPSByZXF1aXJlKCdodHRwJykKICAgICwgc2VydmVyID0gaHR0cC5jcmVhdGVTZXJ2ZXIoYXBwKQogICAgLCBpbyA9IHJlcXVpcmUoJ3NvY2tldC5pbycpLmxpc3RlbihzZXJ2ZXIpOwoKc2VydmVyLmxpc3Rlbig2OTY5KTsKCi8vIHJvdXRpbmcKYXBwLmdldCgnLycsIGZ1bmN0aW9uIChyZXEsIHJlcykKewogICAgcmVzLnNlbmRmaWxlKC8qX19kaXJuYW1lICsqLyAnaW5kZXguaHRtbCcpOwp9KTsKCi8vIHVzZXJuYW1lcyB3aGljaCBhcmUgY3VycmVudGx5IGNvbm5lY3RlZCB0byB0aGUgY2hhdAp2YXIgdXNlcm5hbWVzID0ge307CgoKLy8gcm9vbXMgd2hpY2ggYXJlIGN1cnJlbnRseSBhdmFpbGFibGUgaW4gY2hhdAp2YXIgcm9vbXMgPSBbJ3Jvb20xJywgJ3Jvb20yJywgJ3Jvb20zJ107Cgppby5zb2NrZXRzLm9uKCdjb25uZWN0aW9uJywgZnVuY3Rpb24gKHNvY2tldCkKewogICAgLy8gd2hlbiB0aGUgY2xpZW50IGVtaXRzICdhZGR1c2VyJywgdGhpcyBsaXN0ZW5zIGFuZCBleGVjdXRlcwogICAgc29ja2V0Lm9uKCdhZGR1c2VyJywgZnVuY3Rpb24gKHVzZXJuYW1lKQogICAgeyAgICAgICAgCiAgICAgICAgc29ja2V0LnVzZXJuYW1lID0gdXNlcm5hbWU7ICAgICAvLyBzdG9yZSB0aGUgdXNlcm5hbWUgaW4gdGhlIHNvY2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oKSB7CiAgICAgICAgLy8gcmVtb3ZlIHRoZSB1c2VybmFtZSBmcm9tIGdsb2JhbCB1c2VybmFtZXMgbGlzdAogICAgICAgIGRlbGV0ZSB1c2VybmFtZXNbc29ja2V0LnVzZXJuYW1lXTsKICAgICAgICAvLyB1cGRhdGUgbGlzdCBvZiB1c2VycyBpbiBjaGF0LCBjbGllbnQtc2lkZQogICAgICAgIGlvLnNvY2tldHMuZW1pdCgndXBkYXRldXNlcnMnLCB1c2VybmFtZXMpOwogICAgICAgIC8vIGVjaG8gZ2xvYmFsbHkgdGhhdCB0aGlzIGNsaWVudCBoYXMgbGVmdAogICAgICAgIHNvY2tldC5icm9hZGNhc3QuZW1pdCgndXBkYXRlY2hhdCcsICdTRVJWRVInLCBzb2NrZXQudXNlcm5hbWUgKyAnIGhhcyBkaXNjb25uZWN0ZWQnKTsKICAgICAgICBzb2NrZXQubGVhdmUoc29ja2V0LnJvb20pOwogICAgfSk7Cn0pOw=="},"asBuffer":null},"loaded":true}}
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
var express = require('express')
    , app = express()
    , http = require('http')
    , server = http.createServer(app)
    , io = require('socket.io').listen(server);

server.listen(6969);

// routing
app.get('/', function (req, res)
{
    res.sendfile(/*__dirname +*/ 'index.html');
});

// usernames which are currently connected to the chat
var usernames = {};


// rooms which are currently available in chat
var rooms = ['room1', 'room2', 'room3'];

io.sockets.on('connection', function (socket)
{
    // when the client emits 'adduser', this listens and executes
    socket.on('adduser', function (username)
    {        
        socket.username = username;     // store the username in the socket session for this client        
        socket.room = 'room1';          // store the room name in the socket session for this client        
        usernames[username] = username; // add the client's username to the global list        
        socket.join('room1');           // send client to room 1        
        socket.emit('updatechat', 'SERVER', 'you have connected to room1'); // echo to client they've connected        
        socket.broadcast.to('room1').emit('updatechat', 'SERVER', username + ' has connected to this room'); // echo to room 1 that a person has connected to their room
        socket.emit('updaterooms', rooms, 'room1');
    });

    // when the client emits 'sendchat', this listens and executes
    socket.on('sendchat', function (data)
    {
        // we tell the client to execute 'updatechat' with 2 parameters
        console.log(data);
        io.sockets.in(socket.room).emit('updatechat', socket.username, data.data);
        
 
    });

    socket.on('switchRoom', function (newroom)
    {
        socket.leave(socket.room);
        socket.join(newroom);
        socket.emit('updatechat', 'SERVER', 'you have connected to ' + newroom);
        // sent message to OLD room
        socket.broadcast.to(socket.room).emit('updatechat', 'SERVER', socket.username + ' has left this room');
        // update socket session room title
        socket.room = newroom;
        socket.broadcast.to(newroom).emit('updatechat', 'SERVER', socket.username + ' has joined this room');
        socket.emit('updaterooms', rooms, newroom);
    });


    // when the user disconnects.. perform this
    socket.on('disconnect', function () {
        // remove the username from global usernames list
        delete usernames[socket.username];
        // update list of users in chat, client-side
        io.sockets.emit('updateusers', usernames);
        // echo globally that this client has left
        socket.broadcast.emit('updatechat', 'SERVER', socket.username + ' has disconnected');
        socket.leave(socket.room);
    });
});
node v10.16.0