Skip to content
This repository has been archived by the owner on Mar 29, 2021. It is now read-only.

Commit

Permalink
Use express for HTTP server
Browse files Browse the repository at this point in the history
  • Loading branch information
auchenberg committed Nov 22, 2016
1 parent 0ab75d2 commit b32b692
Showing 1 changed file with 40 additions and 40 deletions.
80 changes: 40 additions & 40 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
//

import * as http from 'http';
import * as express from 'express';
import * as ws from 'ws';
import { Server as WebSocketServer } from 'ws';
import { EventEmitter } from 'events';
Expand All @@ -15,6 +16,7 @@ import { IIOSProxySettings } from './adapters/adapterInterfaces';

export class ProxyServer extends EventEmitter {
private _hs: http.Server;
private _es: express;
private _wss: WebSocketServer;
private _serverPort: number;
private _adapter: Adapter;
Expand All @@ -30,10 +32,15 @@ export class ProxyServer extends EventEmitter {
this._serverPort = serverPort;
this._clients = new Map<ws, string>();

this._hs = http.createServer((a, b) => this.onServerRequest(a, b));
this._wss = new WebSocketServer({ server: this._hs });
this._es = express()
this._hs = http.createServer(this._es)
this._wss = new WebSocketServer({
server: this._hs
});
this._wss.on('connection', (a) => this.onWSSConnection(a));

this.setupHttpHandlers();

// Start server and return the port number
this._hs.listen(this._serverPort);
const port = this._hs.address().port;
Expand All @@ -60,54 +67,47 @@ export class ProxyServer extends EventEmitter {
this._adapter.stop();
}

private onServerRequest(request: http.IncomingMessage, response: http.ServerResponse): void {
// Normalize request url
let url = request.url.trim().toLocaleLowerCase();
if (url.lastIndexOf('/') === url.length - 1) {
url = url.substr(0, url.length - 1);
}
Logger.log(`server.onServerRequest`, url);
private setupHttpHandlers(): void {

// This is a work around to the fact that the server does not always refresh as expected
// We still parse the json as normal, but also kill and restart the server
if (url === '/refresh') {
this._es.get('/', (req, res) => {
res.json({
msg: 'Hello from RemoteDebug iOS WebKit Adapter'
})
})

this._es.get('/refresh', (req, res) => {
this._adapter.forceRefresh();
this.emit('forceRefresh');
response.writeHead(200, { 'Content-Type': 'text/html' });
response.end();
} else if (url === ('/json') || url === '/json/list') {
// Respond with json
res.json({
status: 'ok'
})
})

this._es.get('/json', (req, res) => {
this._adapter.getTargets().then((targets) => {
response.writeHead(200, { 'Content-Type': 'application/json; charset=UTF-8' });
response.write(JSON.stringify(targets, null, 2));
response.end();
res.json(targets)
});
} else if (url === '/json/version') {
})

let data = [
this._es.get('/json/list', (req, res) => {
this._adapter.getTargets().then((targets) => {
res.json(targets)
});
})

this._es.get('/json/version', (req, res) => {
res.json([
{
'Browser': 'Safari',
'Protocol-Version': '1.2'
}
];

response.writeHead(200, { 'Content-Type': 'application/json; charset=UTF-8' });
response.write(JSON.stringify(data, null, 2));
response.end();
} else if (url === '/protocol.json') {
// Write out protocol.json file
response.writeHead(200, { 'Content-Type': 'application/json; charset=UTF-8' });
response.end();
} else if (url === '' || url === '/') {
// Respond with attach page
response.writeHead(200, { 'Content-Type': 'text/html' });
response.write('Hello from RemoteDebug iOS WebKit Adapter');
response.end();
} else {
// Not found
response.writeHead(404, { 'Content-Type': 'text/html' });
response.end();
}
])
})

this._es.get('/json/protocol', (req, res) => {
res.json()
})

}

private onWSSConnection(ws: ws): void {
Expand Down

0 comments on commit b32b692

Please sign in to comment.