1
1
/* Web Sockets */
2
2
/*
3
- - Web sockets allow the creation of connections between clients and server.
4
- - On these connections data can flow (back adn forward) in real time
5
- */
3
+ - Web sockets allow the creation of connections between clients and server.
4
+ - On these connections, data can flow (back and forward) in real time
5
+ */
6
6
7
7
/* Socket.io */
8
8
/*
9
- - Great tool for real-time applications
10
- - Provides fall-backs in case the client doesn't support web sockets
9
+ - Great tool for real-time applications
10
+ - Provides fallbacks in case the client doesn't support web sockets
11
11
12
- Install by running:
13
- npm install --save socket.io
14
- */
12
+ Install by running:
13
+ npm install --save socket.io
14
+ */
15
15
var express = require ( 'express' ) ;
16
16
var app = express ( ) ;
17
17
// Create an http server and dispatch the requests to express
@@ -20,34 +20,34 @@ var server = require('http').createServer(app);
20
20
var io = require ( 'socket.io' ) ( server ) ;
21
21
22
22
// Listen for connection events (on socket.io) and define the callback function
23
- io . on ( 'connection' , function ( client ) {
23
+ io . on ( 'connection' , function ( client ) {
24
24
console . log ( 'Client connected...' ) ;
25
25
// Emits the 'messages' event on the clients and sends the object {socket:io}
26
- client . emit ( 'messages' , { socket :'i.o' } )
26
+ client . emit ( 'messages' , { socket : 'i.o' } )
27
27
28
- client . on ( 'fromClient' , function ( data ) {
28
+ client . on ( 'fromClient' , function ( data ) {
29
29
console . log ( data ) ;
30
30
// Use broadcast to send a message to all the clients that are connected
31
- client . broadcast . emit ( "fromServer" , { fromServer :'fromServer' } ) ;
31
+ client . broadcast . emit ( "fromServer" , { fromServer : 'fromServer' } ) ;
32
32
} ) ;
33
33
34
34
} )
35
35
36
- app . get ( '/' , function ( req , res ) {
36
+ app . get ( '/' , function ( req , res ) {
37
37
res . sendFile ( __dirname + '/socket.html' ) ;
38
38
} ) ;
39
39
40
40
server . listen ( 8080 ) ;
41
41
42
42
/*
43
- Run the following cmd to start node and create the scoket.io server:
44
- node '.\ 13 - Socket.io.js'
43
+ Run the following cmd to start node and create the scoket.io server:
44
+ node '13 - Socket.io.js'
45
45
46
- - See socket.html for client-side code
46
+ - See socket.html for client-side code
47
47
48
- On your browser: localhost:8080
48
+ On your browser: localhost:8080
49
49
50
- Expected response:
50
+ Expected response:
51
51
- console.log with "i.o", numbers should be printed
52
52
- On node, the numbers should keep counting up
53
53
*/
0 commit comments