Skip to content

Commit 1a8fe37

Browse files
committed
updated websocket example with example html page.
1 parent 805577b commit 1a8fe37

File tree

3 files changed

+114
-14
lines changed

3 files changed

+114
-14
lines changed

README.md

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,23 @@ var proxy = proxyMiddleware('/api', {target: 'http://www.example.org'});
2222
// context options
2323

2424
// 'proxy' is now ready to be used in a server.
25-
26-
// shorthand syntax for the example above:
27-
// proxyMiddleware('http://www.example.org/api');
28-
2925
```
3026
* **context**: matches provided context against request-urls' path.
3127
Matching requests will be proxied to the target host.
3228
Example: `'/api'` or `['/api', '/ajax']`. (more about [context matching](#context-matching))
3329
* **options.target**: target host to proxy to.
3430
Check out available [proxy middleware options](#options).
3531

32+
``` javascript
33+
// shorthand syntax for the example above:
34+
var proxy = proxyMiddleware('http://www.example.org/api');
35+
36+
```
37+
More about the [shorthand configuration](#shorthand).
3638

3739

3840
### Example
39-
A simple example with express server.
41+
An example with express server.
4042
```javascript
4143
// include dependencies
4244
var express = require('express');
@@ -97,7 +99,7 @@ Request URL's [ _path-absolute_ and _query_](https://tools.ietf.org/html/rfc3986
9799

98100

99101
### Shorthand
100-
Use the shorthand syntax for simple use cases. The `context` and `option.target` will be automatically configured when shorthand is used. Options can still be used if needed.
102+
Use the shorthand syntax when verbose configuration is not needed. The `context` and `option.target` will be automatically configured when shorthand is used. Options can still be used if needed.
101103

102104
```javascript
103105
proxyMiddleware('http://www.example.org:8000/api');
@@ -147,7 +149,7 @@ var server = app.listen(3000);
147149
}
148150
```
149151

150-
* **option.proxyTable**: object, re-target `option.target` based on the request header `host` parameter. `host` can be used in conjunction with `path`. The order of the configuration matters.
152+
* **option.proxyTable**: object, re-target `option.target` based on the request header `host` parameter. `host` can be used in conjunction with `path`. Only one instance of the proxy will be used. The order of the configuration matters.
151153
```javascript
152154
{
153155
"integration.localhost:3000" : "http://localhost:8001", // host only
@@ -216,9 +218,9 @@ $ node examples/connect
216218
```
217219

218220
Or just explore the proxy examples' sources:
219-
* `examples/connect` - [connect proxy middleware example](https://github.com/chimurai/http-proxy-middleware/tree/master/examples/connect/index.js)
220-
* `examples/express` - [express proxy middleware example](https://github.com/chimurai/http-proxy-middleware/tree/master/examples/express/index.js)
221-
* `examples/browser-sync` - [browser-sync proxy middleware example](https://github.com/chimurai/http-proxy-middleware/tree/master/examples/browser-sync/index.js)
221+
* `examples/connect` - [connect proxy example](https://github.com/chimurai/http-proxy-middleware/tree/master/examples/connect/index.js)
222+
* `examples/express` - [express proxy example](https://github.com/chimurai/http-proxy-middleware/tree/master/examples/express/index.js)
223+
* `examples/browser-sync` - [browser-sync proxy example](https://github.com/chimurai/http-proxy-middleware/tree/master/examples/browser-sync/index.js)
222224
* `examples/websocket` - [websocket proxy example](https://github.com/chimurai/http-proxy-middleware/tree/master/examples/websocket/index.js) with express
223225
224226

examples/websocket/index.html

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>http-proxy-middleware - WebSocket example</title>
6+
<style>
7+
fieldset {
8+
border: 0;
9+
}
10+
label {
11+
display: inline-block;
12+
width: 5em;
13+
vertical-align: top;
14+
}
15+
code {
16+
background-color: #eee;
17+
}
18+
</style>
19+
</head>
20+
21+
<body>
22+
23+
<h2>WebSocket demo</h2>
24+
25+
<p>
26+
Proxy <code>ws://localhost:3000</code> to <code>ws://echo.websocket.org</code>
27+
</p>
28+
29+
<fieldset id="configuration">
30+
<p>
31+
<label for="location">location:</label>
32+
<input id="location" type="text" disabled="disabled" value="ws://localhost:3000">
33+
<button id="connect">connect</button>
34+
<button id="disconnect" disabled="disabled">disconnect</button>
35+
</p>
36+
</fieldset>
37+
<fieldset id="messaging" disabled="disabled">
38+
<p>
39+
<label for="message">message:</label>
40+
<input type="text" id="message" value="Hello WebSocket">
41+
<button id="send">send</button>
42+
</p>
43+
<p>
44+
<label for="logger">log:</label>
45+
<textarea id="logger" cols="30" rows="10"></textarea>
46+
</p>
47+
</fieldset>
48+
49+
<script>
50+
window.onload = function () {
51+
// elements
52+
var configuration = document.getElementById('configuration');
53+
var location = document.getElementById('location');
54+
var connect = document.getElementById('connect');
55+
var disconnect = document.getElementById('disconnect');
56+
var messaging = document.getElementById('messaging');
57+
var message = document.getElementById('message');
58+
var send = document.getElementById('send');
59+
var logger = document.getElementById('logger');
60+
61+
// ws
62+
var socket;
63+
64+
connect.onclick = function () {
65+
connect.disabled = true;
66+
disconnect.disabled = false;
67+
messaging.disabled = false;
68+
69+
socket = new WebSocket('ws://localhost:3000');
70+
socket.onopen = function () { log('CONNECTED'); };
71+
socket.onclose = function () { log('DISCONNECTED'); };
72+
socket.onerror = function () { log('SOCKET ERROR OCCURED'); };
73+
socket.onmessage = function (msg) { log('RECEIVED:' + msg.data); };
74+
}
75+
76+
disconnect.onclick = function () {
77+
connect.disabled = false;
78+
disconnect.disabled = true;
79+
messaging.disabled = true;
80+
socket.close();
81+
}
82+
83+
send.onclick = function () {
84+
log('SEND: ' + message.value);
85+
socket.send(message.value);
86+
};
87+
88+
function log (message) {
89+
logger.value = logger.value + message + '\n'
90+
}
91+
92+
}
93+
</script>
94+
95+
</body>
96+
</html>

examples/websocket/index.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ var proxyMiddleware = require('../../index'); // require('h
88
// context: '/' will proxy all requests
99
var proxy = proxyMiddleware('/', {
1010
target: 'http://echo.websocket.org',
11-
// target: 'ws://echo.websocket.org', // alternative way to provide target with ws:// protocol
1211
// pathRewrite: {
1312
// '^/websocket' : '/socket', // rewrite path.
1413
// '^/removepath' : '' // remove path.
@@ -19,20 +18,23 @@ var proxy = proxyMiddleware('/', {
1918
});
2019

2120
var app = express();
21+
app.use('/', express.static(__dirname)); // demo page
2222
app.use(proxy); // add the proxy to express
2323

24-
app.listen(3000);
24+
var server = app.listen(3000);
25+
server.on('upgrade', proxy.upgrade); // optional: upgrade externally
2526

2627
console.log('listening on port 3000');
2728
console.log('try:');
29+
console.log(' http://localhost:3000 for a demo');
2830
console.log(' ws://localhost:3000 requests will be proxied to ws://echo.websocket.org');
2931

3032
/**
3133
* Example:
32-
* Open http://localhost:3000 in WebSocket compatible browser. // don't mind the 404 page...
34+
* Open http://localhost:3000 in WebSocket compatible browser.
3335
* In browser console:
3436
* 1. `var socket = new WebSocket('ws://localhost:3000');` // create new WebSocket
3537
* 2. `socket.onmessage = function (msg) {console.log(msg)};` // listen to socket messages
36-
* 3. `socket.send('hello world')`; // send message
38+
* 3. `socket.send('hello world');` // send message
3739
* > {data: "hello world"} // server should echo back your message.
3840
**/

0 commit comments

Comments
 (0)