-
Notifications
You must be signed in to change notification settings - Fork 570
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
Try to load uws by default, fall back to ws #459
Conversation
@@ -10,7 +10,7 @@ const REPORTER = 'dot'; | |||
gulp.task('default', ['transpile']); | |||
|
|||
gulp.task('test', ['nsp', 'lint'], function () { | |||
if (parseInt(process.versions.node, 10) < 4 && process.env.EIO_WS_ENGINE === 'uws') { | |||
if (parseInt(process.versions.node, 10) < 4 && process.env.EIO_WS_ENGINE === 'ws') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't it be !==
here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed !
Well the message in the gulpfile is wrong now, let me fix it. |
wsModule = require(this.wsEngine); | ||
} catch (ex) { | ||
this.wsEngine = 'ws'; | ||
wsModule = require(this.wsEngine); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
According to the comment above // keep require('ws') as separate expression for packers (browserify, etc)
, shouldn't it rather be wsModule = require('ws');
here?
Related: #418
@@ -47,10 +47,13 @@ | |||
"mocha": "2.3.4", | |||
"s": "0.1.1", | |||
"superagent": "0.15.4", | |||
"uws": "0.4.0" | |||
"uws": "0.12.0" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't the "uws" in dev dependencies be removed?
Forwarded test failure to https://github.com/uWebSockets/uWebSockets/issues/365. |
var eioc = require('engine.io-client');
var listen = require('./common').listen;
var expect = require('expect.js');
describe('server', function () {
it('should allow client reconnect after restarting (ws)', function (done) {
var opts = { transports: ['websocket'] };
var engine = listen(opts, function (port) {
// these two lines make the difference between pass or fail
engine.httpServer.close();
engine.httpServer.listen(port);
var socket = new eioc.Socket('ws://localhost:%d'.s(port), { transports: ['websocket'] });
engine.once('connection', function (conn) {
setTimeout(function () {
conn.close();
}, 10);
});
socket.once('close', function (reason) {
expect(reason).to.be('transport close');
done();
});
});
});
}); This is the only failing test and it passes if you comment out the .close and .listen. This test is very strange and the httpServer being closed and listened on is not part of uws but engine.io itself. I cannot see why it would fail. |
I've found the issue. Engine.io calls close on the websocket server whenever anything calls close on the httpServer. Engine.io then expects to reuse this closed server, which is not the case in uws. You need to fix this in Engine.io - you are not allowed to close a server and use it later. I would just remove those two lines since they make no sense to have in the first place. |
@alexhultman I guess it was implemented like node.js native |
Engineio controls the webspcket module with handleUpgrade and close. The websocket module doesnt care aboit http listen/close in this case - it only cares about the fact that you call websocket module close followed by handleUpgrade. You cannot use a websocket module after closing it. You have to remove or replace this failing test because it fails due to invalid usage of uws. |
Because it is now an optionalDependency, it's either installed or not installable anyway.
@alexhultman thank you for your clear answer. @darrachequesne the problem happened to be, IMO, in engine.io, so i tried to fixed it properly in 04bde4c and rebased against latest master. |
The failure
happens on master as well, not related to this PR. |
The ping failure is an old issue not related to uws. It happens with both ws and uws at random times due to too small timeout. Increase the timer for that case. |
Seems to pass, but let me remove this commit and make another PR just for that. |
Yep :) |
@kapouer @alexhultman thanks! |
Why is it an optional dependency? I heard this means it won't get downloaded by default and thus not really be used in most cases. Wouldn't it be better to make it a real dependency? Installation of uws cannot fail, it always succeeds silently. If someones server suddenly would blow up they could report an issue and use wsEngine: ws until it gets fixed. |
Hi, |
Oh, my bad then |
Pushing the reasoning further i would say |
but you can consider this a new year's joke. |
Even better, I could make it default to ws 👍 |
I disagree, when you use const wss = new WebSocket.Server({ noServer: true }); you want an external server to be used and, in fact, On the other hand Now 2.0.0 is out and I think we should at least fix #473. Something like this should work diff --git a/lib/server.js b/lib/server.js
index 0da4c23..99dfb41 100644
--- a/lib/server.js
+++ b/lib/server.js
@@ -61,6 +61,8 @@ function Server (opts) {
compression.threshold = 1024;
}
});
+
+ this.init();
}
/**
@@ -104,23 +106,24 @@ Server.prototype.clients;
*/
Server.prototype.init = function () {
- if (~this.transports.indexOf('websocket')) {
- var wsModule;
- try {
- wsModule = require(this.wsEngine);
- } catch (ex) {
- this.wsEngine = 'ws';
- // keep require('ws') as separate expression for packers (browserify, etc)
- wsModule = require('ws');
- }
- var WebSocketServer = wsModule.Server;
- this.ws = new WebSocketServer({
- noServer: true,
- clientTracking: false,
- perMessageDeflate: this.perMessageDeflate,
- maxPayload: this.maxHttpBufferSize
- });
+ if (!~this.transports.indexOf('websocket')) return;
+
+ if (this.ws) this.ws.close();
+
+ var wsModule;
+ try {
+ wsModule = require(this.wsEngine);
+ } catch (ex) {
+ this.wsEngine = 'ws';
+ // keep require('ws') as separate expression for packers (browserify, etc)
+ wsModule = require('ws');
}
+ this.ws = new wsModule.Server({
+ noServer: true,
+ clientTracking: false,
+ perMessageDeflate: this.perMessageDeflate,
+ maxPayload: this.maxHttpBufferSize
+ });
};
/** |
That's fine for me, as long as it works :) |
Another option would be to leave everything as is and specify that var engine = require('engine.io');
var server = new engine.Server();
server.init();
server.on('connection', function(socket){
socket.send('hi');
});
// …
httpServer.on('upgrade', function(req, socket, head){
server.handleUpgrade(req, socket, head);
});
httpServer.on('request', function(req, res){
server.handleRequest(req, res);
}); |
Merge #459 (release 2.0.0) changed default wsEngine to uws. Updating README to reflect this change.
Fixes: #432.
There are some test failures before/after so i'm not sure what to do with them.