File tree 5 files changed +178
-0
lines changed
5 files changed +178
-0
lines changed Original file line number Diff line number Diff line change
1
+ var ws = require ( "nodejs-websocket" ) ;
2
+ console . log ( "Start building a connection..." ) ;
3
+
4
+ //init
5
+ var pagesConn = null ,
6
+ pagesServer = null ,
7
+ pagesConnReady = false ,
8
+ pagesServerReady = false ;
9
+
10
+ //create server
11
+ var server = ws . createServer ( function ( conn ) {
12
+
13
+ //Messages received
14
+ conn . on ( "text" , function ( str ) {
15
+ console . log ( "Messages received:" + str ) ;
16
+ if ( str === "connection" ) {
17
+ pagesConn = conn ;
18
+ pagesConnReady = true ;
19
+ conn . sendText ( "success" ) ;
20
+ }
21
+ if ( str === "server" ) {
22
+ pagesServer = conn ;
23
+ pagesServerReady = true ;
24
+ }
25
+ if ( pagesConnReady && pagesServerReady ) {
26
+ pagesServer . sendText ( str ) ;
27
+ }
28
+ conn . sendText ( str ) ;
29
+ } ) ;
30
+
31
+ //close
32
+ conn . on ( "close" , function ( code , reason ) {
33
+ console . log ( "Close connection" ) ;
34
+ } ) ;
35
+
36
+ //error
37
+ conn . on ( "error" , function ( code , reason ) {
38
+ console . log ( "error" ) ;
39
+ } ) ;
40
+ } ) . listen ( 8001 ) ;
41
+
42
+ //为这两页面开个路由
43
+ //实现点击之后的内容存起来
44
+
45
+
46
+ console . log ( "WebSocket success" ) ;
Original file line number Diff line number Diff line change
1
+ <!doctype html>
2
+ < html lang ="en ">
3
+
4
+ < head >
5
+ < meta charset ="UTF-8 ">
6
+ < title > connection</ title >
7
+ < style >
8
+ # connection {
9
+ text-align : center;
10
+ padding : 20px ;
11
+ }
12
+ .allitems {
13
+ text-align : center;
14
+ margin-top : 50px ;
15
+ }
16
+ .allitems div {
17
+ width : 60% ;
18
+ padding : 20px ;
19
+ margin-bottom : 20px ;
20
+ border : 1px solid;
21
+ text-align : center;
22
+ display : inline-block;
23
+ }
24
+ </ style >
25
+ </ head >
26
+
27
+ < body >
28
+ < div id ="connection "> connection...</ div >
29
+ < div class ="allitems ">
30
+ < div > This is the first module!</ div >
31
+ < div > This is the second module!</ div >
32
+ < div > This is the third modele!</ div >
33
+ </ div >
34
+
35
+ < script >
36
+ var mess = document . getElementById ( "connection" ) ;
37
+ if ( window . WebSocket ) {
38
+ var ws = new WebSocket ( 'ws://localhost:8001' ) ;
39
+
40
+ ws . onopen = function ( e ) {
41
+ console . log ( "Connect the server to success" ) ;
42
+ ws . send ( "connection" ) ;
43
+ }
44
+ ws . onclose = function ( e ) {
45
+ console . log ( "Close pages" ) ;
46
+ }
47
+ ws . onerror = function ( e ) {
48
+ console . log ( "This is a error" ) ;
49
+ }
50
+
51
+ ws . onmessage = function ( e ) {
52
+ mess . innerHTML = "Connection success!"
53
+ document . querySelector ( ".allitems" ) . onclick = function ( e ) {
54
+ var time = new Date ( ) ;
55
+ ws . send ( time + "click“" + e . target . innerHTML + "”" ) ;
56
+ }
57
+ }
58
+ }
59
+ </ script >
60
+ </ body >
61
+
62
+ </ html >
Original file line number Diff line number Diff line change
1
+ {
2
+ "name" : " hello-websocket" ,
3
+ "version" : " 1.0.0" ,
4
+ "description" : " " ,
5
+ "main" : " app.js" ,
6
+ "scripts" : {
7
+ "test" : " echo \" Error: no test specified\" && exit 1"
8
+ },
9
+ "author" : " " ,
10
+ "license" : " ISC" ,
11
+ "dependencies" : {
12
+ "nodejs-websocket" : " ^1.7.1"
13
+ }
14
+ }
Original file line number Diff line number Diff line change
1
+ <!doctype html>
2
+ < html lang ="en ">
3
+ < head >
4
+ < meta charset ="UTF-8 ">
5
+ < title > server</ title >
6
+ < style >
7
+ .kuang {
8
+ text-align : center;
9
+ margin-top : 200px ;
10
+ }
11
+ # server {
12
+ text-align : center
13
+ }
14
+ </ style >
15
+ </ head >
16
+ < body >
17
+ < div id ="server "> </ div >
18
+
19
+ < script >
20
+ var server = document . getElementById ( "server" ) ;
21
+ if ( window . WebSocket ) {
22
+ var ws = new WebSocket ( 'ws://localhost:8001' ) ;
23
+
24
+ ws . onopen = function ( e ) {
25
+ console . log ( "success" ) ;
26
+ ws . send ( "server" ) ;
27
+ }
28
+ ws . onclose = function ( e ) {
29
+ console . log ( "closed" ) ;
30
+ }
31
+ ws . onerror = function ( ) {
32
+ console . log ( "this is error" ) ;
33
+ }
34
+
35
+ ws . onmessage = function ( e ) {
36
+ var time = new Date ( ) ;
37
+ server . innerHTML += time + "的消息:" + e . data + "<br>"
38
+ }
39
+ }
40
+ </ script >
41
+ </ body >
42
+
43
+ </ html >
You can’t perform that action at this time.
0 commit comments