Skip to content

Commit 279efb2

Browse files
committed
Fix bad flush when state is connecting
1 parent 5e12dd0 commit 279efb2

File tree

2 files changed

+43
-43
lines changed

2 files changed

+43
-43
lines changed

lib/scsocket.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,9 @@ SCSocket.prototype._onSCOpen = function (status) {
524524
self.processPendingSubscriptions();
525525
});
526526

527-
this._flushEmitBuffer();
527+
if (this.state == this.OPEN) {
528+
this._flushEmitBuffer();
529+
}
528530
};
529531

530532
SCSocket.prototype._onSCError = function (err) {
@@ -717,7 +719,6 @@ SCSocket.prototype._emit = function (event, data, callback) {
717719
}, this.ackTimeout);
718720

719721
this._emitBuffer.append(eventNode);
720-
721722
if (this.state == this.OPEN) {
722723
this._flushEmitBuffer();
723724
}

lib/sctransport.js

Lines changed: 40 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ var BadConnectionError = scErrors.BadConnectionError;
2222

2323

2424
var SCTransport = function (authEngine, codecEngine, options) {
25+
var self = this;
26+
2527
this.state = this.CLOSED;
2628
this.auth = authEngine;
2729
this.codec = codecEngine;
@@ -35,53 +37,14 @@ var SCTransport = function (authEngine, codecEngine, options) {
3537
this._callbackMap = {};
3638
this._batchSendList = [];
3739

38-
this.open();
39-
};
40-
41-
SCTransport.prototype = Object.create(Emitter.prototype);
42-
43-
SCTransport.CONNECTING = SCTransport.prototype.CONNECTING = 'connecting';
44-
SCTransport.OPEN = SCTransport.prototype.OPEN = 'open';
45-
SCTransport.CLOSED = SCTransport.prototype.CLOSED = 'closed';
46-
47-
SCTransport.prototype.uri = function () {
48-
var query = this.options.query || {};
49-
var schema = this.options.secure ? 'wss' : 'ws';
50-
51-
if (this.options.timestampRequests) {
52-
query[this.options.timestampParam] = (new Date()).getTime();
53-
}
54-
55-
query = querystring.encode(query);
56-
57-
if (query.length) {
58-
query = '?' + query;
59-
}
60-
61-
var host;
62-
if (this.options.host) {
63-
host = this.options.host;
64-
} else {
65-
var port = '';
66-
67-
if (this.options.port && ((schema == 'wss' && this.options.port != 443)
68-
|| (schema == 'ws' && this.options.port != 80))) {
69-
port = ':' + this.options.port;
70-
}
71-
host = this.options.hostname + port;
72-
}
73-
74-
return schema + '://' + host + this.options.path + query;
75-
};
76-
77-
SCTransport.prototype.open = function () {
78-
var self = this;
40+
// Open the connection.
7941

8042
this.state = this.CONNECTING;
8143
var uri = this.uri();
8244

8345
var wsSocket = createWebSocket(uri, this.options);
8446
wsSocket.binaryType = this.options.binaryType;
47+
8548
this.socket = wsSocket;
8649

8750
wsSocket.onopen = function () {
@@ -124,6 +87,42 @@ SCTransport.prototype.open = function () {
12487
}, this.connectTimeout);
12588
};
12689

90+
SCTransport.prototype = Object.create(Emitter.prototype);
91+
92+
SCTransport.CONNECTING = SCTransport.prototype.CONNECTING = 'connecting';
93+
SCTransport.OPEN = SCTransport.prototype.OPEN = 'open';
94+
SCTransport.CLOSED = SCTransport.prototype.CLOSED = 'closed';
95+
96+
SCTransport.prototype.uri = function () {
97+
var query = this.options.query || {};
98+
var schema = this.options.secure ? 'wss' : 'ws';
99+
100+
if (this.options.timestampRequests) {
101+
query[this.options.timestampParam] = (new Date()).getTime();
102+
}
103+
104+
query = querystring.encode(query);
105+
106+
if (query.length) {
107+
query = '?' + query;
108+
}
109+
110+
var host;
111+
if (this.options.host) {
112+
host = this.options.host;
113+
} else {
114+
var port = '';
115+
116+
if (this.options.port && ((schema == 'wss' && this.options.port != 443)
117+
|| (schema == 'ws' && this.options.port != 80))) {
118+
port = ':' + this.options.port;
119+
}
120+
host = this.options.hostname + port;
121+
}
122+
123+
return schema + '://' + host + this.options.path + query;
124+
};
125+
127126
SCTransport.prototype._onOpen = function () {
128127
var self = this;
129128

0 commit comments

Comments
 (0)