Leaning up the initial connection, will trigger post-connection messages instead

This commit is contained in:
James Barnsley
2019-04-25 14:49:51 +12:00
parent 860b364b76
commit a9dad3e648
7 changed files with 28 additions and 56 deletions

3
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,3 @@
{
"python.pythonPath": "/usr/local/bin/python"
}

View File

@ -306,23 +306,21 @@ class IrisCore(pykka.ThreadingActor):
return response return response
def add_connection(self, *args, **kwargs): def add_connection(self, *args, **kwargs):
connection_id = kwargs.get('connection_id', None)
connection = kwargs.get('connection', None) connection = kwargs.get('connection', None)
client = kwargs.get('client', None) client = kwargs.get('client', None)
new_connection = { self.connections[client['connection_id']] = {
'client': client, 'client': client,
'connection_id': connection_id, 'id': client['connection_id'],
'connection': connection 'connection': connection
} }
self.connections[connection_id] = new_connection
self.send_message(data={ self.send_message(data={
'recipient': connection_id, 'recipient': client['connection_id'],
'method': 'connection_added', 'method': 'connection_established',
'params': { 'params': {
'connection': { 'connection': {
'connection_id': connection_id, 'connection_id': client['connection_id'],
'client_id': client['client_id'], 'client_id': client['client_id'],
'username': client['username'], 'username': client['username'],
'ip': client['ip'] 'ip': client['ip']

View File

@ -33,32 +33,22 @@ class WebsocketHandler(tornado.websocket.WebSocketHandler):
def open(self): def open(self):
# decode our connection protocol value (which is a payload of id/name from javascript) # Get the client's IP. If it's local, then use it's proxy origin
protocolElements = mem.iris.digest_protocol(self.request.headers.get('Sec-Websocket-Protocol', []))
connection_id = protocolElements['connection_id']
client_id = protocolElements['client_id']
self.connection_id = connection_id
username = protocolElements['username']
created = datetime.strftime(datetime.now(), '%Y-%m-%d %H:%M:%S')
# get our IP
# if it's local, then check for proxy origin
ip = self.request.remote_ip ip = self.request.remote_ip
if (ip == '127.0.0.1' and hasattr(self.request.headers,'X-Forwarded-For')): if (ip == '127.0.0.1' and hasattr(self.request.headers,'X-Forwarded-For')):
ip = self.request.headers['X-Forwarded-For'] ip = self.request.headers['X-Forwarded-For']
# construct our client object, and add to our list of connections # Construct our initial client object, and add to our list of connections
client = { client = {
'client_id': client_id, 'connection_id': mem.iris.generateGuid(),
'connection_id': connection_id, 'client_id': mem.iris.generateGuid(),
'username': username,
'ip': ip, 'ip': ip,
'created': created 'created': datetime.strftime(datetime.now(), '%Y-%m-%d %H:%M:%S')
} }
# add to connections self.connection_id = client['connection_id']
mem.iris.add_connection(connection_id=connection_id, connection=self, client=client)
mem.iris.add_connection(connection=self, client=client)
def on_message(self, message): def on_message(self, message):

View File

@ -72120,6 +72120,9 @@ var PusherMiddleware = function () {
} else { } else {
switch (message.method) { switch (message.method) {
case 'connection_extablished':
store.dispatch(pusherActions.setUsername(store.getState().pusher.username));
break;
case 'connection_added': case 'connection_added':
store.dispatch(pusherActions.connectionAdded(message.params.connection)); store.dispatch(pusherActions.connectionAdded(message.params.connection));
break; break;
@ -72274,24 +72277,12 @@ var PusherMiddleware = function () {
store.dispatch({ type: 'PUSHER_CONNECTING' }); store.dispatch({ type: 'PUSHER_CONNECTING' });
var state = store.getState(); var state = store.getState();
var connection = {
client_id: helpers.generateGuid(),
connection_id: helpers.generateGuid(),
username: 'Anonymous'
};
if (state.pusher.username) {
connection.username = state.pusher.username;
}
connection.username = connection.username.replace(/\W/g, '');
socket = new WebSocket('ws' + (window.location.protocol === 'https:' ? 's' : '') + '://' + state.mopidy.host + ':' + state.mopidy.port + '/iris/ws/', [connection.client_id, connection.connection_id, connection.username]); socket = new WebSocket('ws' + (window.location.protocol === 'https:' ? 's' : '') + '://' + state.mopidy.host + ':' + state.mopidy.port + '/iris/ws/');
socket.onopen = function () { socket.onopen = function () {
store.dispatch({ store.dispatch({
type: 'PUSHER_CONNECTED', type: 'PUSHER_CONNECTED'
connection_id: connection.connection_id,
client_id: connection.client_id,
username: connection.username
}); });
}; };

File diff suppressed because one or more lines are too long

View File

@ -100,7 +100,7 @@
// Release details // Release details
// These are automatically injected to built HTML // These are automatically injected to built HTML
var build = "1555971818"; var build = "1556157415";
var version = "3.36.1"; var version = "3.36.1";
// Construct the script tag // Construct the script tag

View File

@ -75,6 +75,9 @@ const PusherMiddleware = (function(){
} else { } else {
switch (message.method){ switch (message.method){
case 'connection_extablished':
store.dispatch(pusherActions.setUsername(store.getState().pusher.username));
break;
case 'connection_added': case 'connection_added':
store.dispatch(pusherActions.connectionAdded(message.params.connection)); store.dispatch(pusherActions.connectionAdded(message.params.connection));
break; break;
@ -228,27 +231,14 @@ const PusherMiddleware = (function(){
store.dispatch({type: 'PUSHER_CONNECTING'}); store.dispatch({type: 'PUSHER_CONNECTING'});
var state = store.getState(); var state = store.getState();
var connection = {
client_id: helpers.generateGuid(),
connection_id: helpers.generateGuid(),
username: 'Anonymous'
}
if (state.pusher.username){
connection.username = state.pusher.username;
}
connection.username = connection.username.replace(/\W/g, '');
socket = new WebSocket( socket = new WebSocket(
'ws'+(window.location.protocol === 'https:' ? 's' : '')+'://'+state.mopidy.host+':'+state.mopidy.port+'/iris/ws/', 'ws'+(window.location.protocol === 'https:' ? 's' : '')+'://'+state.mopidy.host+':'+state.mopidy.port+'/iris/ws/'
[ connection.client_id, connection.connection_id, connection.username ]
); );
socket.onopen = () => { socket.onopen = () => {
store.dispatch({ store.dispatch({
type: 'PUSHER_CONNECTED', type: 'PUSHER_CONNECTED'
connection_id: connection.connection_id,
client_id: connection.client_id,
username: connection.username
}); });
}; };