diff --git a/openlp/plugins/remotes/html/WebSocketEvents.js b/openlp/plugins/remotes/html/WebSocketEvents.js deleted file mode 100644 index 9a1053273..000000000 --- a/openlp/plugins/remotes/html/WebSocketEvents.js +++ /dev/null @@ -1,113 +0,0 @@ -/****************************************************************************** - * OpenLP - Open Source Lyrics Projection * - * --------------------------------------------------------------------------- * - * Copyright (c) 2008-2014 Raoul Snyman * - * Portions copyright (c) 2008-2014 Tim Bentley, Gerald Britton, Jonathan * - * Corwin, Samuel Findlay, Michael Gorven, Scott Guerrieri, Matthias Hub, * - * Meinert Jordan, Armin Köhler, Erik Lundin, Edwin Lunando, Brian T. Meyer. * - * Joshua Miller, Stevan Pettit, Andreas Preikschat, Mattias Põldaru, * - * Christian Richter, Philip Ridout, Simon Scudder, Jeffrey Smith, * - * Maikel Stuivenberg, Martin Thompson, Jon Tibble, Dave Warnock, * - * Frode Woldsund, Martin Zibricky * - * --------------------------------------------------------------------------- * - * This program is free software; you can redistribute it and/or modify it * - * under the terms of the GNU General Public License as published by the Free * - * Software Foundation; version 2 of the License. * - * * - * This program is distributed in the hope that it will be useful, but WITHOUT * - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for * - * more details. * - * * - * You should have received a copy of the GNU General Public License along * - * with this program; if not, write to the Free Software Foundation, Inc., 59 * - * Temple Place, Suite 330, Boston, MA 02111-1307 USA * - ******************************************************************************/ - -/* Thanks to Ismael Celis for the original idea */ - -var wsEventEngine = function(url, polling_function, polling_interval) -{ - this.polling_handle = null; - this.polling_interval = polling_interval; - this.polling_function = polling_function; - this.retry_handle = null; - this.callbacks = {}; - - this.fallback = function(){ - this.kill_polling(); - if(this.polling_function) - this.polling_handle = window.setInterval(this.polling_function, this.polling_interval); - this.kill_retries(); - var theEngine = this; - this.retry_handle = window.setInterval(function(){theEngine.setup();}, 10000); - } - - this.kill_polling = function(){ - if(this.polling_handle) - window.clearInterval(this.polling_handle); - this.polling_handle = null; - } - - this.kill_retries = function(){ - if(this.retry_handle) - window.clearInterval(this.retry_handle); - } - - this.bind = function(event_name, callback){ - this.callbacks[event_name] = this.callbacks[event_name] || []; - this.callbacks[event_name].push(callback); - return this; - } - - this.send = function(event_name, event_data){ - var payload = JSON.stringify({ event: event_name, data: event_data }); - this.websocket.send(payload); - return this; - } - - this.dispatch = function(event_name, message){ - var chain = this.callbacks[event_name]; - if(typeof chain == 'undefined') return; // no callbacks - for(var i = 0; i < chain.length; i++) - chain[i](message); - } - - this.setup = function(){ - this.websocket = new WebSocket(url); - this.websocket.engine = this; - - this.websocket.onmessage = function(websocket_msg){ - if(this.engine.polling_function) - this.engine.polling_function(); - if( websocket_msg.data.length > 0 ){ - try{ - var json = JSON.parse(websocket_msg.data); - this.engine.dispatch(json.event, json.data); - } - catch(err){ - } - } - } - - this.websocket.onclose = function(){ - this.engine.dispatch('close', null); - this.engine.fallback(); - } - - this.websocket.onopen = function(){ - this.engine.dispatch('open', null); - this.engine.kill_polling(); - this.engine.kill_retries(); - } - - } - - if('WebSocket' in window){ - this.setup(); - } - else{ - this.fallback(); - } - -} \ No newline at end of file diff --git a/openlp/plugins/remotes/html/index.html b/openlp/plugins/remotes/html/index.html index 432fdaa7a..80eaf22e8 100644 --- a/openlp/plugins/remotes/html/index.html +++ b/openlp/plugins/remotes/html/index.html @@ -35,7 +35,6 @@ - - # - # - #
start:
- # - # - # - - manager = WebSocketManager() - manager.start() - # Create a socket (SOCK_STREAM means a TCP socket) - sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - # Fake a handshake - uid = uuid.uuid4() - key = base64.encodebytes(uid.bytes).strip() - data = ('\r\n'.join(WEB_SOCKET_CLIENT_HEADERS).format(host='localhost', port='8888', key=key)).encode('utf-8') - received = None - try: - # Connect to server and send data - sock.connect(('localhost', PORT)) - sock.send(data) - received = sock.recv(1024) - time.sleep(5) - manager.send("broadcast") - print("received: ", ThreadedWebSocketHandler.decode_client_websocket_message(sock.recv(1024))) - time.sleep(2) - manager.send("\r\njust before kill") - print("received: ", ThreadedWebSocketHandler.decode_client_websocket_message(sock.recv(1024))) - time.sleep(2) - finally: - sock.close() - manager.stop() - diff --git a/openlp/plugins/remotes/remoteplugin.py b/openlp/plugins/remotes/remoteplugin.py index 038b3d269..7b82c1fd1 100644 --- a/openlp/plugins/remotes/remoteplugin.py +++ b/openlp/plugins/remotes/remoteplugin.py @@ -33,7 +33,7 @@ from PyQt4 import QtGui from openlp.core.lib import Plugin, StringContent, translate, build_icon from openlp.core.common import Registry -from openlp.plugins.remotes.lib import RemoteTab, OpenLPServer, WebSocketManager +from openlp.plugins.remotes.lib import RemoteTab, OpenLPServer log = logging.getLogger(__name__) @@ -61,7 +61,6 @@ class RemotesPlugin(Plugin): self.icon = build_icon(self.icon_path) self.weight = -1 self.server = None - self.websocketserver = None def initialise(self): """ @@ -70,9 +69,6 @@ class RemotesPlugin(Plugin): log.debug('initialise') super(RemotesPlugin, self).initialise() self.server = OpenLPServer() - self.websocketserver = WebSocketManager() - self.websocketserver.start() - Registry().register_function('websock_send', self.websocketserver.send) if not hasattr(self, 'remote_server_icon'): self.remote_server_icon = QtGui.QLabel(self.main_window.status_bar) size_policy = QtGui.QSizePolicy(QtGui.QSizePolicy.Fixed, QtGui.QSizePolicy.Fixed) @@ -98,9 +94,6 @@ class RemotesPlugin(Plugin): if self.server: self.server.stop_server() self.server = None - if self.websocketserver: - self.websocketserver.stop() - self.websocketserver = None def about(self): """ diff --git a/tests/functional/openlp_plugins/remotes/test_websocket.py b/tests/functional/openlp_plugins/remotes/test_websocket.py deleted file mode 100644 index 37fbd4ab7..000000000 --- a/tests/functional/openlp_plugins/remotes/test_websocket.py +++ /dev/null @@ -1,123 +0,0 @@ -# -*- coding: utf-8 -*- -# vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4 - -############################################################################### -# OpenLP - Open Source Lyrics Projection # -# --------------------------------------------------------------------------- # -# Copyright (c) 2008-2014 Raoul Snyman # -# Portions copyright (c) 2008-2014 Tim Bentley, Gerald Britton, Jonathan # -# Corwin, Samuel Findlay, Michael Gorven, Scott Guerrieri, Matthias Hub, # -# Meinert Jordan, Armin Köhler, Erik Lundin, Edwin Lunando, Brian T. Meyer. # -# Joshua Miller, Stevan Pettit, Andreas Preikschat, Mattias Põldaru, # -# Christian Richter, Philip Ridout, Simon Scudder, Jeffrey Smith, # -# Maikel Stuivenberg, Martin Thompson, Jon Tibble, Dave Warnock, # -# Frode Woldsund, Martin Zibricky, Patrick Zimmermann # -# --------------------------------------------------------------------------- # -# This program is free software; you can redistribute it and/or modify it # -# under the terms of the GNU General Public License as published by the Free # -# Software Foundation; version 2 of the License. # -# # -# This program is distributed in the hope that it will be useful, but WITHOUT # -# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or # -# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for # -# more details. # -# # -# You should have received a copy of the GNU General Public License along # -# with this program; if not, write to the Free Software Foundation, Inc., 59 # -# Temple Place, Suite 330, Boston, MA 02111-1307 USA # -############################################################################### -""" -This module contains tests for WebSockets -""" -import base64 -import uuid -import socket -import time -from unittest import TestCase - -from openlp.plugins.remotes.lib.websocket import WebSocketManager, ThreadedWebSocketHandler, \ - WEB_SOCKET_CLIENT_HEADERS -from tests.functional import MagicMock, patch, mock_open - - -class TestWebSockets(TestCase): - """ - Test the functions in the :mod:`lib` module. - """ - - def setUp(self): - """ - Setup the WebSocketsManager - """ - self.manager = WebSocketManager() - self.manager.start() - - def tearDown(self): - self.manager.stop() - - def attempt_to_talk_with_no_handshake_test(self): - """ - Test the websocket without handshaking first - """ - # GIVEN: A default configuration - - # WHEN: attempts to talk without upgrading to websocket - # Create a socket (SOCK_STREAM means a TCP socket) - sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - data = bytes('No upgrade', 'utf-8') - received = None - try: - # Connect to server and send data - sock.connect(('localhost', 8888)) - sock.send(data) - # Receive data from the server and shut down - received = sock.recv(1024) - finally: - sock.close() - - # THEN: - self.assertIs(isinstance(self.manager, WebSocketManager), True, - 'It should be an object of WebSocketsManager type') - self.assertRegexpMatches(received.decode('utf-8'), '.*Error:.*', 'Mismatch') - - def handshake_and_talk_test(self): - """ - Test the websocket handshake - """ - # GIVEN: A default configuration - - # WHEN: upgrade to websocket and then talk - print("starting the websocket server") - print("started") - # Create a socket (SOCK_STREAM means a TCP socket) - sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - # Fake a handshake - uid = uuid.uuid4() - key = base64.encodebytes(uid.bytes).strip() - data = bytes('\r\n'.join(WEB_SOCKET_CLIENT_HEADERS).format(host='localhost', port='8888', key=key), 'utf-8') - received = None - try: - # Connect to server and send data - sock.connect(('localhost', 8888)) - print("connected") - sock.send(data) - #print("data sent: ", data.decode('utf-8')) - # Receive data from the server and shut down - time.sleep(1) - received = sock.recv(1024) - print("data received: ", received.decode('utf-8')) - time.sleep(1) - self.manager.send('broadcast') - time.sleep(1) - received_broadcast = sock.recv(1024) - print(received_broadcast) - decoded_broadcast = ThreadedWebSocketHandler.decode_client_websocket_message(received_broadcast) - finally: - time.sleep(1) - sock.close() - - # THEN: - self.assertIs(isinstance(self.manager, WebSocketManager), True, - 'It should be an object of WebSocketsManager type') - self.assertRegexpMatches(received.decode('utf-8'), '.*Upgrade: websocket.*', 'Handshake failed') - self.assertRegexpMatches(decoded_broadcast, '.*broadcast', 'WebSocket did not receive correct string')