Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Encapsulate sockjs, introduce ws and build base for users to make own server implementation #1904

Merged
merged 7 commits into from May 28, 2019
9 changes: 2 additions & 7 deletions lib/Server.js
Expand Up @@ -31,7 +31,7 @@ const createDomain = require('./utils/createDomain');
const runBonjour = require('./utils/runBonjour');
const routes = require('./utils/routes');
const schema = require('./options.json');
const SockJSServer = require('./servers/sockjsServer');
const SockJSServer = require('./servers/SockJSServer');

// Workaround for node ^8.6.0, ^9.0.0
// DEFAULT_ECDH_CURVE is default to prime256v1 in these version
Expand Down Expand Up @@ -654,12 +654,7 @@ class Server {
}

createSocketServer() {
this.socketServer = new SockJSServer({
error: this.log.error.bind(this),
debug: this.log.debug.bind(this),
server: this.listeningApp,
path: this.sockPath,
});
this.socketServer = new SockJSServer(this);

this.socketServer.onConnection((connection) => {
if (!connection) {
Expand Down
6 changes: 5 additions & 1 deletion lib/servers/baseServer.js
Expand Up @@ -2,4 +2,8 @@

// base class that users should extend if they are making their own
// server implementation
module.exports = class BaseServer {};
module.exports = class BaseServer {
constructor(server) {
this.server = server;
}
};
14 changes: 7 additions & 7 deletions lib/servers/sockjsServer.js
Expand Up @@ -5,7 +5,7 @@
func-names
*/
const sockjs = require('sockjs');
const BaseServer = require('./baseServer');
const BaseServer = require('./BaseServer');

// Workaround for sockjs@~0.3.19
// sockjs will remove Origin header, however Origin header is required for checking host.
Expand All @@ -29,23 +29,23 @@ const BaseServer = require('./baseServer');

module.exports = class SockJSServer extends BaseServer {
// options has: error (function), debug (function), server (http/s server), path (string)
constructor(options) {
super();
constructor(server) {
super(server);
this.socket = sockjs.createServer({
// Use provided up-to-date sockjs-client
sockjs_url: '/__webpack_dev_server__/sockjs.bundle.js',
// Limit useless logs
log: (severity, line) => {
if (severity === 'error') {
options.error(line);
this.server.log.error(line);
} else {
options.debug(line);
this.server.log.debug(line);
}
},
});

this.socket.installHandlers(options.server, {
prefix: options.path,
this.socket.installHandlers(this.server.listeningApp, {
prefix: this.server.sockPath,
});
}

Expand Down
4 changes: 2 additions & 2 deletions lib/servers/wsServer.js
@@ -1,8 +1,8 @@
'use strict';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The file name should be changed from wsServer.js to WsServer.


// const ws = require('ws');
const BaseServer = require('./baseServer');
const BaseServer = require('./BaseServer');

// ws server implementation under construction
// will need changes in the client as well to function
module.exports = class WSServer extends BaseServer {};
module.exports = class WsServer extends BaseServer {};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/cc @hiroppy What do you think is better:

  • WebsocketServer
  • WSServer
  • WsServer

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like WebsocketServer. For example, the plugin name of wasm(WebAssemblyPlugin) which webpack has is not omitted. But this example is webpack, not webpack-dev-server. In any case, I think that it is easier to understand if it is not omitted.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 let's use WebsocketServer

/cc @Loonride