File tree Expand file tree Collapse file tree 3 files changed +75
-0
lines changed
Expand file tree Collapse file tree 3 files changed +75
-0
lines changed Original file line number Diff line number Diff line change 1+ <!doctype html>
2+ < html >
3+ < head >
4+ < title > Chat Application using node.js /socket.io</ title >
5+ < style >
6+ * { margin : 0 ; padding : 0 ; box-sizing : border-box; }
7+ body { font : 13px Helvetica, Arial; }
8+ form { background : # 000 ; padding : 3px ; position : fixed; bottom : 0 ; width : 100% ; }
9+ form input { border : 0 ; padding : 10px ; width : 90% ; margin-right : .5% ; }
10+ form button { width : 9% ; background : rgb (130 , 224 , 255 ); border : none; padding : 10px ; }
11+ # messages { list-style-type : none; margin : 0 ; padding : 0 ; }
12+ # messages li { padding : 5px 10px ; }
13+ # messages li : nth-child (odd) { background : # eee ; }
14+ </ style >
15+ </ head >
16+ < body >
17+ < ul id ="messages "> </ ul >
18+ < form action ="">
19+ < input id ="m " autocomplete ="off " /> < button > Send</ button >
20+ </ form >
21+ Broadcast Message:< div id ="custommessages "> </ div >
22+ < script src ="/socket.io/socket.io.js "> </ script >
23+ < script src ="http://code.jquery.com/jquery-1.11.1.js "> </ script >
24+ < script >
25+ var socket = io ( ) ;
26+ $ ( 'form' ) . submit ( function ( ) {
27+ socket . emit ( 'chat message' , $ ( '#m' ) . val ( ) ) ;
28+ $ ( '#m' ) . val ( '' ) ;
29+ return false ;
30+ } ) ;
31+ socket . on ( 'chat message' , function ( msg ) {
32+ $ ( '#messages' ) . append ( $ ( '<li>' ) . text ( msg ) ) ;
33+ } ) ;
34+ socket . on ( 'customechat' , function ( msg ) {
35+ $ ( '#custommessages' ) . html ( msg ) ;
36+ } ) ;
37+ </ script >
38+ </ body >
39+ </ html >
Original file line number Diff line number Diff line change 1+ var app = require ( 'express' ) ( ) ;
2+ var http = require ( 'http' ) . Server ( app ) ;
3+ var socket_io = require ( 'socket.io' ) ( http ) ;
4+
5+ app . get ( '/' , function ( req , res ) {
6+ res . sendfile ( 'index.html' ) ;
7+ } ) ;
8+
9+
10+
11+ socket_io . on ( 'connection' , function ( socket ) {
12+ console . log ( 'user Connected' ) ;
13+ socket . on ( 'disconnect' , function ( ) {
14+ console . log ( 'user disconnected' ) ;
15+ } ) ;
16+ socket . on ( 'chat message' , function ( msg ) {
17+ socket_io . emit ( 'chat message' , msg ) ;
18+ } ) ;
19+ var i = 0 ;
20+ setInterval ( function ( ) {
21+ socket . broadcast . emit ( 'customechat' , "BD : " + ( i ++ ) ) ;
22+ } , 500 )
23+ } ) ;
24+
25+ http . listen ( 3000 , function ( ) {
26+ console . log ( 'listening on *:3000' ) ;
27+ } ) ;
Original file line number Diff line number Diff line change 1+ {
2+ "name" : " socket-chat-example" ,
3+ "version" : " 0.0.1" ,
4+ "description" : " my first socket.io app" ,
5+ "dependencies" : {
6+ "express" : " ^4.4.0" ,
7+ "socket.io" : " ^1.0.3"
8+ }
9+ }
You can’t perform that action at this time.
0 commit comments