File tree 4 files changed +144
-0
lines changed
4 files changed +144
-0
lines changed Original file line number Diff line number Diff line change
1
+ # A Real Time Chat Built on Node-js and Socket.io
2
+
3
+ ------------------------------------------------------------------
4
+
5
+ ### Setup your Chat Room
6
+
7
+ ------------------------------------------------------------------
8
+
9
+ Install node-js. Just follow this guide https://github.com/joyent/node/wiki/Installation .
10
+ Install the Node Pacakge Manager (NPM). Read this https://github.com/isaacs/npm .
11
+
12
+ ------------------------------------------------------------------
13
+
14
+ Get socket.io via NPM by running:
15
+
16
+ ``` bash
17
+ ~ $ npm install socket.io
18
+ ```
19
+
20
+ ------------------------------------------------------------------
21
+
22
+ Start the node-js server. Just type:
23
+
24
+ ``` bash
25
+ ~ $ node chat.js
26
+ ```
27
+
28
+ ------------------------------------------------------------------
29
+
30
+ Open the chat.html file in your favourite browser and enjoy!
Original file line number Diff line number Diff line change
1
+ < html >
2
+ < head >
3
+ < link rel ="stylesheet " type ="text/css " href ="styles.css "/>
4
+ < script src ="http://localhost:3000/socket.io/socket.io.js "> </ script >
5
+ < script src ="http://code.jquery.com/jquery-1.7.1.js " type ="text/javascript "> </ script >
6
+ < script type ="text/javascript ">
7
+
8
+ var socket = io . connect ( 'http://localhost:3000' ) ;
9
+
10
+ socket . on ( 'msg' , function ( data ) {
11
+ var msg = JSON . parse ( data ) ;
12
+ appendMsg ( msg ) ;
13
+ } ) ;
14
+
15
+ socket . on ( 'init' , function ( data ) {
16
+ var messages = JSON . parse ( data )
17
+ for ( i in messages )
18
+ appendMsg ( messages [ i ] )
19
+ } ) ;
20
+
21
+ function appendMsg ( msg ) {
22
+ $ ( '#msgs' ) . append ( function ( ) {
23
+ var div = $ ( '<div>' ) ;
24
+ div . html ( '<b>' + msg . username + ':</b> ' + msg . message ) ;
25
+ return div ;
26
+ } ) ;
27
+ $ ( '#msgs' ) [ 0 ] . scrollTop = $ ( '#msgs' ) [ 0 ] . scrollHeight ;
28
+ }
29
+
30
+ function sendMsg ( ) {
31
+ var msg = { } ;
32
+ $ . each ( $ ( '#chat' ) . serializeArray ( ) , function ( i , v ) {
33
+ msg [ v . name ] = v . value ;
34
+ } ) ;
35
+ $ ( "#msg" ) . val ( "" ) ;
36
+ appendMsg ( msg ) ;
37
+ socket . emit ( 'msg' , JSON . stringify ( msg ) ) ;
38
+ }
39
+ </ script >
40
+ </ head >
41
+ < body >
42
+ < div >
43
+ < p class ="chat-title " onclick ="javascript: toggleChat(); "> Real Time Chat</ p >
44
+ < div id ="msgs "> </ div >
45
+ < div id ="form ">
46
+ < form id ="chat " action ="javascript:sendMsg() ">
47
+ < label for ="username "> Username: </ label > < input name ="username " type ="text "> < br />
48
+ < label for ="msg "> Message: </ label > < input id ="msg " type ="text " name ="message "/> < br />
49
+ < input type ="submit "/>
50
+ </ form >
51
+ </ div >
52
+ </ div >
53
+ </ body >
54
+ </ html >
Original file line number Diff line number Diff line change
1
+ var DEBUG = true
2
+ var PORT = 3000
3
+ var INIT_MESSAGES = 5
4
+
5
+ var http = require ( 'http' )
6
+
7
+ var server = http . createServer ( )
8
+ server . listen ( PORT )
9
+
10
+ var io = require ( 'socket.io' ) . listen ( server )
11
+
12
+ var messages = new Array ( )
13
+
14
+ Array . prototype . inject = function ( element ) {
15
+
16
+ if ( this . length >= INIT_MESSAGES ) {
17
+ this . shift ( )
18
+ }
19
+ this . push ( element )
20
+ }
21
+
22
+ io . sockets . on ( 'connection' , function ( client ) {
23
+
24
+ if ( DEBUG )
25
+ console . log ( "New Connection: " , client . id )
26
+
27
+ client . emit ( "init" , JSON . stringify ( messages ) )
28
+
29
+ client . on ( 'msg' , function ( msg ) {
30
+
31
+ if ( DEBUG )
32
+ console . log ( "Message: " + msg )
33
+
34
+ var message = JSON . parse ( msg )
35
+ messages . inject ( message )
36
+
37
+ client . broadcast . emit ( 'msg' , msg )
38
+ } )
39
+
40
+ client . on ( 'disconnect' , function ( ) {
41
+
42
+ if ( DEBUG )
43
+ console . log ( "Disconnected: " , client . id )
44
+ } )
45
+ } )
Original file line number Diff line number Diff line change
1
+ # msgs {
2
+ height : 300px ;
3
+ background : # f3f3ff ;
4
+ font-family : monospace, sans;
5
+ overflow : auto;
6
+ }
7
+
8
+ .chat-title {
9
+ color : white;
10
+ background : # a2a2ee ;
11
+ font-weight : bold;
12
+ font-size : 20px ;
13
+ height : 24px ;
14
+ padding : 2px ;
15
+ }
You can’t perform that action at this time.
0 commit comments