From 6db548fde10fffd5bca98f5485947de379142e45 Mon Sep 17 00:00:00 2001 From: xuyaoqiang Date: Tue, 1 Aug 2017 13:47:01 +0800 Subject: [PATCH 1/4] compatiable fix for namespace messed up --- socketio/packet.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/socketio/packet.py b/socketio/packet.py index dd9f859..c047611 100644 --- a/socketio/packet.py +++ b/socketio/packet.py @@ -1,5 +1,6 @@ import functools import json as _json +from urllib.parse import urlparse import six @@ -87,7 +88,7 @@ class Packet(object): ep = '' self.namespace = None self.data = None - ep = ep[1:] + ep = urlparse(ep[1:]).path dash = (ep + '-').find('-') attachment_count = 0 if ep[0:dash].isdigit(): From 180b8ab7cf2f4c46f4a1d06ac7d5167de8d75a73 Mon Sep 17 00:00:00 2001 From: xuyaoqiang Date: Tue, 1 Aug 2017 16:03:49 +0800 Subject: [PATCH 2/4] reproduce invalid namespace --- examples/sanic/README.rst | 42 --------------- examples/sanic/app.html | 91 ------------------------------- examples/sanic/app.py | 94 ++++++++------------------------- examples/sanic/index.html | 7 +++ examples/sanic/latency.html | 64 ---------------------- examples/sanic/latency.py | 25 --------- examples/sanic/static/style.css | 4 -- 7 files changed, 29 insertions(+), 298 deletions(-) delete mode 100644 examples/sanic/README.rst delete mode 100755 examples/sanic/app.html mode change 100755 => 100644 examples/sanic/app.py create mode 100644 examples/sanic/index.html delete mode 100755 examples/sanic/latency.html delete mode 100755 examples/sanic/latency.py delete mode 100644 examples/sanic/static/style.css diff --git a/examples/sanic/README.rst b/examples/sanic/README.rst deleted file mode 100644 index 53b4160..0000000 --- a/examples/sanic/README.rst +++ /dev/null @@ -1,42 +0,0 @@ -Socket.IO sanic Examples -======================== - -This directory contains example Socket.IO applications that are compatible with -asyncio and the sanic framework. These applications require Python 3.5 or -later. - -Note that Sanic versions older than 0.4.0 do not support the WebSocket -protocol, so on those versions the only available transport is long-polling. - -app.py ------- - -A basic "kitchen sink" type application that allows the user to experiment -with most of the available features of the Socket.IO server. - -latency.py ----------- - -A port of the latency application included in the official Engine.IO -Javascript server. In this application the client sends *ping* messages to -the server, which are responded by the server with a *pong*. The client -measures the time it takes for each of these exchanges and plots these in real -time to the page. - -This is an ideal application to measure the performance of the different -asynchronous modes supported by the Socket.IO server. - -Running the Examples --------------------- - -To run these examples, create a virtual environment, install the requirements -and then run:: - - $ python app.py - -or:: - - $ python latency.py - -You can then access the application from your web browser at -``http://localhost:8000``. diff --git a/examples/sanic/app.html b/examples/sanic/app.html deleted file mode 100755 index 1668814..0000000 --- a/examples/sanic/app.html +++ /dev/null @@ -1,91 +0,0 @@ - - - - Flask-SocketIO Test - - - - - -

Flask-SocketIO Test

-

Send:

-
- - -
-
- - -
-
- - -
-
- - -
-
- - - -
-
- - -
-
- -
-

Receive:

-

- - diff --git a/examples/sanic/app.py b/examples/sanic/app.py old mode 100755 new mode 100644 index 8eeb689..e1bb6e7 --- a/examples/sanic/app.py +++ b/examples/sanic/app.py @@ -1,93 +1,43 @@ -import asyncio +#! /usr/bin/env python +# -*- coding: utf-8 -*- +""" +@author: xuyaoqiang +@contact: xuyaoqiang@gmail.com +@date: 2017-07-28 15:29 +@version: 0.0.0 +@license: +@copyright: + +""" from sanic import Sanic -from sanic.response import html - +from sanic.response import html import socketio + sio = socketio.AsyncServer(async_mode='sanic') app = Sanic() sio.attach(app) -async def background_task(): - """Example of how to send server generated events to clients.""" - count = 0 - while True: - await sio.sleep(10) - count += 1 - await sio.emit('my response', {'data': 'Server generated event'}, - namespace='/test') +class TestNamespace(socketio.AsyncNamespace): + def on_connect(self, sid, environ): + pass -@app.listener('before_server_start') -def before_server_start(sanic, loop): - sio.start_background_task(background_task) + def on_disconnect(self, sid): + pass @app.route('/') async def index(request): - with open('app.html') as f: + with open('index.html') as f: return html(f.read()) -@sio.on('my event', namespace='/test') -async def test_message(sid, message): - await sio.emit('my response', {'data': message['data']}, room=sid, - namespace='/test') - - -@sio.on('my broadcast event', namespace='/test') -async def test_broadcast_message(sid, message): - await sio.emit('my response', {'data': message['data']}, namespace='/test') - - -@sio.on('join', namespace='/test') -async def join(sid, message): - sio.enter_room(sid, message['room'], namespace='/test') - await sio.emit('my response', {'data': 'Entered room: ' + message['room']}, - room=sid, namespace='/test') - - -@sio.on('leave', namespace='/test') -async def leave(sid, message): - sio.leave_room(sid, message['room'], namespace='/test') - await sio.emit('my response', {'data': 'Left room: ' + message['room']}, - room=sid, namespace='/test') - - -@sio.on('close room', namespace='/test') -async def close(sid, message): - await sio.emit('my response', - {'data': 'Room ' + message['room'] + ' is closing.'}, - room=message['room'], namespace='/test') - await sio.close_room(message['room'], namespace='/test') - - -@sio.on('my room event', namespace='/test') -async def send_room_message(sid, message): - await sio.emit('my response', {'data': message['data']}, - room=message['room'], namespace='/test') - - -@sio.on('disconnect request', namespace='/test') -async def disconnect_request(sid): - await sio.disconnect(sid, namespace='/test') - - -@sio.on('connect', namespace='/test') -async def test_connect(sid, environ): - await sio.emit('my response', {'data': 'Connected', 'count': 0}, room=sid, - namespace='/test') - - -@sio.on('disconnect', namespace='/test') -def test_disconnect(sid): - print('Client disconnected') - - -app.static('/static', './static') +sio.register_namespace(TestNamespace('/test')) if __name__ == '__main__': - app.run() + app.run(port=8001, debug=True) + diff --git a/examples/sanic/index.html b/examples/sanic/index.html new file mode 100644 index 0000000..58a682e --- /dev/null +++ b/examples/sanic/index.html @@ -0,0 +1,7 @@ + + + diff --git a/examples/sanic/latency.html b/examples/sanic/latency.html deleted file mode 100755 index 769e9ef..0000000 --- a/examples/sanic/latency.html +++ /dev/null @@ -1,64 +0,0 @@ - - - - Socket.IO Latency - - - -

Socket.IO Latency

-

(connecting)

- - - - - - - - diff --git a/examples/sanic/latency.py b/examples/sanic/latency.py deleted file mode 100755 index 2e1712f..0000000 --- a/examples/sanic/latency.py +++ /dev/null @@ -1,25 +0,0 @@ -from sanic import Sanic -from sanic.response import html - -import socketio - -sio = socketio.AsyncServer(async_mode='sanic') -app = Sanic() -sio.attach(app) - - -@app.route('/') -def index(request): - with open('latency.html') as f: - return html(f.read()) - - -@sio.on('ping_from_client') -async def ping(sid): - await sio.emit('pong_from_server', room=sid) - -app.static('/static', './static') - - -if __name__ == '__main__': - app.run() diff --git a/examples/sanic/static/style.css b/examples/sanic/static/style.css deleted file mode 100644 index d20bcad..0000000 --- a/examples/sanic/static/style.css +++ /dev/null @@ -1,4 +0,0 @@ -body { margin: 0; padding: 0; font-family: Helvetica Neue; } -h1 { margin: 100px 100px 10px; } -h2 { color: #999; margin: 0 100px 30px; font-weight: normal; } -#latency { color: red; } From 53e5a22a5fb5fa5f0d024a8bff3392c48f3df81a Mon Sep 17 00:00:00 2001 From: xuyaoqiang Date: Tue, 1 Aug 2017 16:55:01 +0800 Subject: [PATCH 3/4] fix urlparse import error --- socketio/packet.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/socketio/packet.py b/socketio/packet.py index c047611..2ad8de4 100644 --- a/socketio/packet.py +++ b/socketio/packet.py @@ -1,6 +1,9 @@ import functools import json as _json -from urllib.parse import urlparse +try: + from urllib.parse import urlparse +except ImportError: + from urllib2.urlparse import urlparse import six From eaa63503e26ca0074403ee4f492d5fbe882b69e5 Mon Sep 17 00:00:00 2001 From: xuyaoqiang Date: Tue, 1 Aug 2017 17:02:50 +0800 Subject: [PATCH 4/4] fix import error --- socketio/packet.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/socketio/packet.py b/socketio/packet.py index 2ad8de4..35ed033 100644 --- a/socketio/packet.py +++ b/socketio/packet.py @@ -3,7 +3,7 @@ import json as _json try: from urllib.parse import urlparse except ImportError: - from urllib2.urlparse import urlparse + from urlparse import urlparse import six