-
Notifications
You must be signed in to change notification settings - Fork 849
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from chimurai/websocket
feat: websocket proxy
- Loading branch information
Showing
8 changed files
with
167 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/** | ||
* Module dependencies. | ||
*/ | ||
var express = require('../../node_modules/express/index'); // require('express'); | ||
var proxyMiddleware = require('../../index'); // require('http-proxy-middleware'); | ||
|
||
// configure proxy middleware | ||
// context: '/' will proxy all requests | ||
var proxy = proxyMiddleware('/', { | ||
target: 'http://echo.websocket.org', | ||
// target: 'ws://echo.websocket.org', // alternative way to provide target with ws:// protocol | ||
// pathRewrite: { | ||
// '^/websocket' : '/socket', // rewrite path. | ||
// '^/removepath' : '' // remove path. | ||
// }, | ||
changeOrigin: true, // for vhosted sites, changes host header to match to target's host | ||
ws: true // enable websocket proxy | ||
|
||
}); | ||
|
||
var app = express(); | ||
app.use(proxy); // add the proxy to express | ||
|
||
app.listen(3000); | ||
|
||
console.log('listening on port 3000'); | ||
console.log('try:'); | ||
console.log(' ws://localhost:3000 requests will be proxied to ws://echo.websocket.org'); | ||
|
||
/** | ||
* Example: | ||
* Open http://localhost:3000 in WebSocket compatible browser. // don't mind the 404 page... | ||
* In browser console: | ||
* 1. `var socket = new WebSocket('ws://localhost:3000');` // create new WebSocket | ||
* 2. `socket.onmessage = function (msg) {console.log(msg)};` // listen to socket messages | ||
* 3. `socket.send('hello world')`; // send message | ||
* > {data: "hello world"} // server should echo back your message. | ||
**/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
var expect = require('chai').expect; | ||
var proxyMiddleware = require('../index'); | ||
var http = require('http'); | ||
var express = require('express'); | ||
var WebSocket = require('ws'); | ||
var WebSocketServer = require('ws').Server; | ||
|
||
describe('websocket proxy', function () { | ||
var proxyServer, ws, wss; | ||
var targetHeaders; | ||
var responseMessage; | ||
|
||
beforeEach(function () { | ||
proxyServer = createServer(3000, proxyMiddleware('/', { | ||
target:'http://localhost:8000', | ||
ws: true, | ||
pathRewrite: { | ||
'^/socket' : '' | ||
} | ||
})); | ||
|
||
wss = new WebSocketServer({ port: 8000 }); | ||
wss.on('connection', function connection(ws) { | ||
ws.on('message', function incoming(message) { | ||
ws.send(message); // echo received message | ||
}); | ||
}); | ||
}); | ||
|
||
beforeEach(function (done) { | ||
// need to make a normal http request, | ||
// so http-proxy-middleware can catch the upgrade request | ||
http.get('http://localhost:3000/', function () { | ||
// do a second http request to make | ||
// sure only 1 listener subscribes to upgrade request | ||
http.get('http://localhost:3000/', function () { | ||
ws = new WebSocket('ws://localhost:3000/socket'); | ||
|
||
ws.on('message', function incoming(message) { | ||
responseMessage = message; | ||
done(); | ||
}); | ||
|
||
ws.on('open', function open() { | ||
ws.send('foobar'); | ||
}); | ||
}); | ||
}); | ||
|
||
}); | ||
|
||
afterEach(function () { | ||
proxyServer.close(); | ||
wss.close(); | ||
ws = null; | ||
}); | ||
|
||
it('should proxy to path', function () { | ||
expect(responseMessage).to.equal('foobar'); | ||
}); | ||
}); | ||
|
||
function createServer (portNumber, middleware) { | ||
var app = express(); | ||
|
||
if (middleware) { | ||
app.use(middleware); | ||
} | ||
|
||
var server = app.listen(portNumber); | ||
|
||
return server; | ||
} |