File tree 6 files changed +132
-0
lines changed
nginx-websockets-practice
6 files changed +132
-0
lines changed 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
+ < meta http-equiv ="X-UA-Compatible " content ="IE=edge ">
6
+ < meta name ="viewport " content ="width=device-width, initial-scale=1.0 ">
7
+ < title > Document</ title >
8
+ </ head >
9
+ < body >
10
+ < h1 > Hello, WebSockets</ h1 >
11
+ < input type = 'text ' id = 'txt '>
12
+ < div id = 'divOut '> </ div >
13
+ < script >
14
+ const txtBox = document . getElementById ( "txt" ) ;
15
+ const divOut = document . getElementById ( "divOut" ) ;
16
+ const ws = new WebSocket ( "ws://localhost/wschat/" )
17
+ ws . onmessage = e => divOut . innerHTML += e . data + "<br/>" ;
18
+ txtBox . addEventListener ( "keypress" , e => {
19
+ if ( e . keyCode === 13 ) {
20
+
21
+ ws . send ( txtBox . value ) ;
22
+ txtBox . value = "" ;
23
+ }
24
+ } )
25
+ </ script >
26
+
27
+ </ body >
28
+ </ html >
Original file line number Diff line number Diff line change
1
+ const http = require ( "http" ) ;
2
+ const WebSocketServer = require ( "websocket" ) . server ;
3
+ const httpServer = http . createServer ( ) ;
4
+ const websocketServer = new WebSocketServer (
5
+ { "httpServer" : httpServer } )
6
+ const PORT = process . argv [ 2 ] || 8080 ;
7
+ let connection = null ;
8
+ httpServer . listen ( PORT , ( ) => console . log ( `Listening on port ${ PORT } ` ) )
9
+
10
+ websocketServer . on ( "request" , request => {
11
+
12
+ connection = request . accept ( null , request . origin ) ;
13
+ connection . on ( "message" , data => {
14
+ console . log ( `Hey I received a message ${ data . utf8Data } ` ) ;
15
+ connection . send ( `Hey Client! Received your message ${ data . utf8Data } on ${ PORT } ` )
16
+ } )
17
+ } )
Original file line number Diff line number Diff line change
1
+ {
2
+ "name" : " nginx-websockets-practice" ,
3
+ "version" : " 1.0.0" ,
4
+ "description" : " " ,
5
+ "main" : " index.js" ,
6
+ "scripts" : {
7
+ "test" : " echo \" Error: no test specified\" && exit 1"
8
+ },
9
+ "author" : " " ,
10
+ "license" : " ISC"
11
+ }
Original file line number Diff line number Diff line change
1
+ stream {
2
+
3
+ upstream wsbackends {
4
+
5
+ server 127.0.0.1:2222 ;
6
+ server 127.0.0.1:3333 ;
7
+ server 127.0.0.1:4444 ;
8
+ server 127.0.0.1:5555 ;
9
+ }
10
+
11
+ server {
12
+ listen 80 ;
13
+ proxy_pass wsbackends;
14
+ }
15
+
16
+
17
+ }
18
+
19
+
20
+ events { }
Original file line number Diff line number Diff line change
1
+ #kill all node processes
2
+ sudo killall node
3
+ #stop nginx
4
+ nginx -s stop
5
+ #kill any dead nginx processes
6
+ sudo killall nginx
7
+ #run node
8
+ node index.js 2222 & node index.js 3333 & node index.js 4444 & node index.js 5555
9
+ #run nginx with a config
10
+ nginx -c /Users/HusseinNasser/javascript/javascript_playground/nginx-websockets/tcp.cfg
Original file line number Diff line number Diff line change
1
+ http {
2
+
3
+ upstream wsapp {
4
+
5
+ server 127.0.0.1:2222 ;
6
+ server 127.0.0.1:3333 ;
7
+ }
8
+
9
+ upstream wschat {
10
+
11
+ server 127.0.0.1:4444 ;
12
+ server 127.0.0.1:5555 ;
13
+
14
+ }
15
+ server {
16
+ listen 80 ;
17
+
18
+ location / {
19
+ root /Users/HusseinNasser/javascript/javascript_playground/nginx-websockets-practice/;
20
+ }
21
+
22
+ location /wsapp {
23
+ proxy_pass http://wsapp;
24
+ proxy_http_version 1.1 ;
25
+ proxy_set_header Upgrade $http_upgrade;
26
+ proxy_set_header Connection "Upgrade";
27
+ proxy_set_header Host $host;
28
+
29
+ }
30
+
31
+ location /wschat {
32
+
33
+ proxy_pass http://wschat;
34
+
35
+ proxy_http_version 1.1 ;
36
+ proxy_set_header Upgrade $http_upgrade;
37
+ proxy_set_header Connection "Upgrade";
38
+ proxy_set_header Host $host;
39
+ }
40
+ }
41
+
42
+
43
+ }
44
+
45
+
46
+ events { }
You can’t perform that action at this time.
0 commit comments