1
+ var express = require ( 'express' ) ,
2
+ http = require ( 'http' ) ;
3
+
4
+ var app = express ( ) ;
5
+ var server = http . createServer ( app ) ;
6
+ var io = require ( 'socket.io' ) . listen ( server , { log : false } ) ;
7
+
8
+ app . use ( "/js" , express . static ( __dirname + '/js' ) ) ;
9
+
10
+ server . listen ( 8080 ) ;
11
+
12
+ // routing
13
+ app . get ( '/' , function ( req , res ) {
14
+ res . sendfile ( __dirname + '/index.html' ) ;
15
+ } ) ;
16
+
17
+ // usernames which are currently connected to the chat
18
+ var usernames = { } ;
19
+ var chatrooms = { } ;
20
+ io . sockets . on ( 'connection' , function ( socket ) {
21
+
22
+ socket . on ( 'sendchat' , function ( data ) {
23
+ // we tell the client to execute 'updatechat with 2 parameters'
24
+ io . sockets . in ( socket . chatroom ) . emit ( 'updatechat' , socket . username , data ) ;
25
+ } ) ;
26
+
27
+ // when the client emits 'adduser', this listens and executes
28
+ socket . on ( 'adduser' , function ( info ) {
29
+ username = info . username ;
30
+ chatroom = info . chatroom ;
31
+
32
+ if ( ! ( usernames [ username ] == undefined || usernames [ username ] == null ) )
33
+ {
34
+ socket . emit ( 'errorConnect' , "The username '" + username + "' is already in use." ) ;
35
+ return ;
36
+ }
37
+ // we store the username in the socket session for this client
38
+ socket . username = username ;
39
+ // we store the room
40
+ socket . chatroom = chatroom ;
41
+ if ( chatrooms [ chatroom ] == undefined )
42
+ {
43
+ chatrooms [ chatroom ] = {
44
+ name : chatroom ,
45
+ usernames : { } ,
46
+ usercount : 0
47
+ }
48
+ }
49
+ // add the client's username to the global list
50
+ usernames [ username ] = username ;
51
+ chatrooms [ chatroom ] . usernames [ username ] = username ;
52
+ chatrooms [ chatroom ] . usercount ++ ;
53
+ socket . join ( chatroom ) ;
54
+
55
+ // echo to the client the chatroom name
56
+ socket . emit ( 'successfullyjoinchat' , chatroom ) ;
57
+ // echo to the client they've connected
58
+ socket . emit ( 'updatechat' , 'SERVER' , 'you have connected' ) ;
59
+ // echo globablly (all clients) that a person has connected
60
+ socket . broadcast . to ( chatroom ) . emit ( 'updatechat' , 'SERVER' , username + ' has connected' ) ;
61
+ // update the list of users in chat, client-side
62
+ io . sockets . in ( chatroom ) . emit ( 'updateusers' , chatrooms [ chatroom ] . usernames ) ;
63
+ } ) ;
64
+
65
+ // when the user disconnects.. perform this
66
+ socket . on ( 'disconnect' , function ( ) {
67
+ // remove the username from global usernames list
68
+ try
69
+ {
70
+ delete usernames [ socket . username ] ;
71
+ delete chatrooms [ socket . chatroom ] . usernames [ username ] ;
72
+ chatrooms [ socket . chatroom ] . usercount -- ;
73
+ // echo globally that this client has left
74
+ if ( chatrooms [ socket . chatroom ] . usercount > 0 )
75
+ {
76
+ io . sockets . in ( socket . chatroom ) . emit ( 'updatechat' , 'SERVER' , socket . username + ' has disconnected' ) ;
77
+ // update list of users in chat, client-side
78
+ io . sockets . in ( socket . chatroom ) . emit ( 'updateusers' , usernames ) ;
79
+ }
80
+
81
+ socket . leave ( socket . chatroom ) ;
82
+ }
83
+ catch ( e )
84
+ {
85
+ console . log ( socket . username , socket . chatroom , chatrooms ) ;
86
+ }
87
+
88
+
89
+ } ) ;
90
+
91
+ } ) ;
0 commit comments