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

Add payload to connect #84

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions hookbox/js_src/hookbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ exports.logging = logging;
logger.setLevel(0);


exports.connect = function(url, cookieString) {
exports.connect = function(url, connectPayload, cookieString) {
if (!url.match('/$')) {
url = url + '/';
}
var p = new HookBoxProtocol(url, cookieString);
var p = new HookBoxProtocol(url, connectPayload, cookieString);
if (window.WebSocket) {
jsioConnect(p, 'websocket', {url: url.replace('http://', 'ws://') + 'ws' });
p.connectionLost = bind(p, '_connectionLost', 'websocket');
Expand Down Expand Up @@ -42,7 +42,6 @@ var Subscription = Class(function(supr) {
this.onState = function(frame) {}

this.frame = function(name, args) {
logger.debug('received frame', name, args);
switch(name) {
case 'PUBLISH':
if (this.historySize) {
Expand Down Expand Up @@ -106,13 +105,13 @@ var Subscription = Class(function(supr) {

HookBoxProtocol = Class([RTJPProtocol], function(supr) {
// Public api
this.onOpen = function() { }
this.onOpen = function(args) { }
this.onClose = function(err, wasConnected) { }
this.onError = function(args) { }
this.onSubscribed = function(name, subscription) { }
this.onUnsubscribed = function(subscription, args) { }
this.onMessaged = function(args) {}
this.init = function(url, cookieString) {
this.init = function(url, connectPayload, cookieString) {
supr(this, 'init', []);
this.url = url;
try {
Expand All @@ -127,6 +126,7 @@ HookBoxProtocol = Class([RTJPProtocol], function(supr) {
this._publishes = [];
this._messages = [];
this._errors = {};
this._connectPayload = connectPayload;
this.username = null;
}

Expand Down Expand Up @@ -158,10 +158,11 @@ HookBoxProtocol = Class([RTJPProtocol], function(supr) {
this.connectionMade = function() {
logger.debug('connectionMade');
this.transport.setEncoding('utf8');
this.sendFrame('CONNECT', { cookie_string: this.cookieString });
this.sendFrame('CONNECT', { cookie_string: this.cookieString, payload: JSON.stringify(this._connectPayload) });
}

this.frameReceived = function(fId, fName, fArgs) {
logger.debug('received frame', fName, fArgs);
switch(fName) {
case 'MESSAGE':
this.onMessaged(fArgs);
Expand All @@ -182,7 +183,7 @@ HookBoxProtocol = Class([RTJPProtocol], function(supr) {
this.message.apply(this, msg);
}

this.onOpen();
this.onOpen(fArgs);
break;
case 'SUBSCRIBE':
if (fArgs.user == this.username) {
Expand Down
3 changes: 1 addition & 2 deletions hookbox/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,8 @@ def frame_CONNECT(self, fid, fargs):
self.cookie_string = fargs['cookie_string']
self.cookies = parse_cookies(fargs['cookie_string'])
self.cookie_id = self.cookies.get(self.cookie_identifier, None)
self.server.connect(self)
self.server.connect(self, fargs.get('payload', 'null'))
self.state = 'connected'
self.send_frame('CONNECTED', { 'name': self.user.get_name() })

def frame_SUBSCRIBE(self, fid, fargs):
if self.state != 'connected':
Expand Down
19 changes: 12 additions & 7 deletions hookbox/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,21 +251,26 @@ def http_request(self, path_name=None, cookie_string=None, form={}, full_path=No

# def _webhook_error

def connect(self, conn):
form = { 'conn_id': conn.id }
def connect(self, conn, payload):
try:
decoded_payload = json.loads(payload)
except:
raise ExpectedException("Invalid json for payload")
form = { 'conn_id': conn.id, 'payload': json.dumps(decoded_payload) }
success, options = self.http_request('connect', conn.get_cookie(), form, conn=conn)
if not success:
raise ExpectedException(options.get('error', 'Unauthorized'))
if 'name' not in options:
user_name = options.pop('name', None)
if not user_name:
raise ExpectedException('Unauthorized (missing name parameter in server response)')
payload = options.pop('override_payload', payload)
self.conns[conn.id] = conn
user = self.get_user(options['name'])
del options['name']
user.update_options(**options)
user = self.get_user(user_name)
user.update_options(**user.extract_valid_options(options))
user.add_connection(conn)
self.admin.user_event('connect', user.get_name(), conn.serialize())
self.admin.connection_event('connect', conn.id, conn.serialize())
#print 'successfully connected', user.name
conn.send_frame('CONNECTED', { 'name': user.get_name(), 'payload': payload })
eventlet.spawn(self.maybe_auto_subscribe, user, options, conn=conn)

def disconnect(self, conn):
Expand Down
Loading