-
Notifications
You must be signed in to change notification settings - Fork 6
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
Upgrade Sugar to Primus 8 #185
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
class SugarClient { | ||
static initClass() { | ||
this.host = null; | ||
this.Primus = null; | ||
} | ||
|
||
constructor(userId, authToken) { | ||
this.primusUrl = this.primusUrl.bind(this); | ||
this.connect = this.connect.bind(this); | ||
this.disconnect = this.disconnect.bind(this); | ||
this.receiveData = this.receiveData.bind(this); | ||
this.subscribeTo = this.subscribeTo.bind(this); | ||
this.unsubscribeFrom = this.unsubscribeFrom.bind(this); | ||
this.on = this.on.bind(this); | ||
this.off = this.off.bind(this); | ||
this.emit = this.emit.bind(this); | ||
this.__subscribeToChannels = this.__subscribeToChannels.bind(this); | ||
this.__subscribeTo = this.__subscribeTo.bind(this); | ||
this.createEvent = this.createEvent.bind(this); | ||
this.userId = userId; | ||
this.authToken = authToken; | ||
this.events = { }; | ||
this.subscriptions = { }; | ||
this.initializePrimus(); | ||
} | ||
|
||
initializePrimus() { | ||
if (SugarClient.Primus == null) { throw 'SugarClient.Primus is not defined'; } | ||
if (SugarClient.host == null) { throw 'SugarClient.host is not defined'; } | ||
this.primus = SugarClient.Primus.connect(SugarClient.host, { | ||
websockets: true, | ||
network: true, | ||
manual: true | ||
} | ||
); | ||
|
||
this.primus.on('outgoing::url', this.primusUrl); | ||
return this.primus.on('data', this.receiveData); | ||
} | ||
|
||
host() { | ||
return SugarClient.host; | ||
} | ||
|
||
primusUrl(baseUrl) { | ||
if (this.userId && this.authToken) { | ||
return baseUrl.query = `user_id=${ this.userId }&auth_token=${ this.authToken }`; | ||
} | ||
} | ||
|
||
connect() { | ||
this.disconnect(); | ||
return this.primus.open(); | ||
} | ||
|
||
disconnect() { | ||
let key; | ||
let userKeys = []; | ||
userKeys = ((() => { | ||
const result = []; | ||
for (key in this.subscriptions) { | ||
const _ = this.subscriptions[key]; | ||
if (key.match(/^(session|user):/i)) { | ||
result.push(key); | ||
} | ||
} | ||
return result; | ||
})()); | ||
for (key of userKeys) { delete this.subscriptions[key]; } | ||
this.userKey = (this.loggedIn = null); | ||
return this.primus.end(); | ||
} | ||
|
||
receiveData(data) { | ||
if (data.type === 'connection') { | ||
if (console && console.info) { | ||
console.info('[CONNECTED] ', data); | ||
} | ||
this.loggedIn = data.loggedIn; | ||
this.userKey = data.userKey; | ||
this.subscriptions[this.userKey] = true; | ||
return setTimeout(this.__subscribeToChannels, 100); | ||
} else { | ||
return this.emit(data); | ||
} | ||
} | ||
|
||
subscribeTo(channel) { | ||
if (this.subscriptions[channel]) { return false; } | ||
this.subscriptions[channel] = true; | ||
return this.__subscribeTo(channel); | ||
} | ||
|
||
unsubscribeFrom(channel) { | ||
if (!this.subscriptions[channel]) { return; } | ||
delete this.subscriptions[channel]; | ||
return this.primus.write({action: 'Unsubscribe', params: { channel }}); | ||
} | ||
|
||
on(type, callback) { | ||
if (!this.events[type]) { this.events[type] = []; } | ||
return this.events[type].push(callback); | ||
} | ||
|
||
off(type, callback) { | ||
if (callback && this.events[type]) { | ||
return this.events[type] = this.events[type].filter(cb => cb !== callback); | ||
} else { | ||
return delete this.events[type]; | ||
} | ||
} | ||
|
||
emit(data) { | ||
const callbacks = this.events[data.type] || []; | ||
return callbacks.map((callback) => callback(data)); | ||
} | ||
|
||
__subscribeToChannels() { | ||
return (() => { | ||
const result = []; | ||
for (let channel in this.subscriptions) { | ||
const _ = this.subscriptions[channel]; | ||
result.push(this.__subscribeTo(channel)); | ||
} | ||
return result; | ||
})(); | ||
} | ||
|
||
__subscribeTo(channel) { | ||
return this.primus.write({action: 'Subscribe', params: { channel }}); | ||
} | ||
|
||
createEvent(type, channel, data) { | ||
return this.primus.write({ | ||
action: 'Event', | ||
params: { type, channel, data }}); | ||
} | ||
} | ||
SugarClient.initClass(); | ||
|
||
if (typeof module !== 'undefined' && module !== null) { | ||
module.exports = SugarClient; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
The only difference between this and https://github.com/zooniverse/Sugar-Client/blob/master/client.js that I'm seeing is the deletion of
ping: 10000
(link). Apologies if it has already been stated somewhere, but is that property no longer needed with Primus 8, and it was with Primus 6?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.
Possibly related - https://github.com/primus/primus#protocol
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.
It’s been removed because the direction of the ping/pong protocol changed from client->server to server->client. There’s a list of client options in the Readme.
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.
So that option said: ping the server then wait 10s for a pong response. The client now sends pong back in response to pings from the server.
You can see the traffic if you watch the web socket in browser dev tools.