extend example
This commit is contained in:
@@ -37,7 +37,9 @@ class SlowSquareClientProtocol(WebSocketClientProtocol):
|
|||||||
self.sendClose()
|
self.sendClose()
|
||||||
|
|
||||||
def onClose(self, wasClean, code, reason):
|
def onClose(self, wasClean, code, reason):
|
||||||
loop.close()
|
if reason:
|
||||||
|
print(reason)
|
||||||
|
loop.stop()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -52,4 +54,4 @@ if __name__ == '__main__':
|
|||||||
coro = loop.create_connection(factory, '127.0.0.1', 9000)
|
coro = loop.create_connection(factory, '127.0.0.1', 9000)
|
||||||
loop.run_until_complete(coro)
|
loop.run_until_complete(coro)
|
||||||
loop.run_forever()
|
loop.run_forever()
|
||||||
# loop.close()
|
loop.close()
|
||||||
|
|||||||
@@ -27,15 +27,22 @@ class SlowSquareServerProtocol(WebSocketServerProtocol):
|
|||||||
|
|
||||||
@asyncio.coroutine
|
@asyncio.coroutine
|
||||||
def slowsquare(self, x):
|
def slowsquare(self, x):
|
||||||
yield from asyncio.sleep(1)
|
if x > 5:
|
||||||
return x * x
|
raise Exception("number too large")
|
||||||
|
else:
|
||||||
|
yield from asyncio.sleep(1)
|
||||||
|
return x * x
|
||||||
|
|
||||||
@asyncio.coroutine
|
@asyncio.coroutine
|
||||||
def onMessage(self, payload, isBinary):
|
def onMessage(self, payload, isBinary):
|
||||||
if not isBinary:
|
if not isBinary:
|
||||||
x = json.loads(payload.decode('utf8'))
|
x = json.loads(payload.decode('utf8'))
|
||||||
res = yield from self.slowsquare(x)
|
try:
|
||||||
self.sendMessage(json.dumps(res).encode('utf8'))
|
res = yield from self.slowsquare(x)
|
||||||
|
except Exception as e:
|
||||||
|
self.sendClose(1000, str(e))
|
||||||
|
else:
|
||||||
|
self.sendMessage(json.dumps(res).encode('utf8'))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
66
examples/asyncio/websocket/slowsquare/server_py2.py
Normal file
66
examples/asyncio/websocket/slowsquare/server_py2.py
Normal file
@@ -0,0 +1,66 @@
|
|||||||
|
###############################################################################
|
||||||
|
##
|
||||||
|
## Copyright (C) 2014 Tavendo GmbH
|
||||||
|
##
|
||||||
|
## Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
## you may not use this file except in compliance with the License.
|
||||||
|
## You may obtain a copy of the License at
|
||||||
|
##
|
||||||
|
## http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
##
|
||||||
|
## Unless required by applicable law or agreed to in writing, software
|
||||||
|
## distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
## See the License for the specific language governing permissions and
|
||||||
|
## limitations under the License.
|
||||||
|
##
|
||||||
|
###############################################################################
|
||||||
|
|
||||||
|
from autobahn.asyncio.websocket import WebSocketServerProtocol, \
|
||||||
|
WebSocketServerFactory
|
||||||
|
|
||||||
|
import asyncio
|
||||||
|
import json
|
||||||
|
|
||||||
|
|
||||||
|
class SlowSquareServerProtocol(WebSocketServerProtocol):
|
||||||
|
|
||||||
|
@asyncio.coroutine
|
||||||
|
def slowsquare(self, x):
|
||||||
|
if x > 5:
|
||||||
|
raise Exception("number too large")
|
||||||
|
else:
|
||||||
|
yield asyncio.sleep(1)
|
||||||
|
raise asyncio.Return(x * x)
|
||||||
|
|
||||||
|
@asyncio.coroutine
|
||||||
|
def onMessage(self, payload, isBinary):
|
||||||
|
if not isBinary:
|
||||||
|
x = json.loads(payload.decode('utf8'))
|
||||||
|
try:
|
||||||
|
res = yield self.slowsquare(x)
|
||||||
|
except Exception as e:
|
||||||
|
self.sendClose(1000, str(e))
|
||||||
|
else:
|
||||||
|
self.sendMessage(json.dumps(res).encode('utf8'))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
|
||||||
|
import asyncio
|
||||||
|
|
||||||
|
factory = WebSocketServerFactory("ws://localhost:9000", debug = False)
|
||||||
|
factory.protocol = SlowSquareServerProtocol
|
||||||
|
|
||||||
|
loop = asyncio.get_event_loop()
|
||||||
|
coro = loop.create_server(factory, '127.0.0.1', 9000)
|
||||||
|
server = loop.run_until_complete(coro)
|
||||||
|
|
||||||
|
try:
|
||||||
|
loop.run_forever()
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
pass
|
||||||
|
finally:
|
||||||
|
server.close()
|
||||||
|
loop.close()
|
||||||
Reference in New Issue
Block a user