refactoring
This commit is contained in:
18
autobahn/autobahn/asyncio/__init__.py
Normal file
18
autobahn/autobahn/asyncio/__init__.py
Normal file
@@ -0,0 +1,18 @@
|
||||
###############################################################################
|
||||
##
|
||||
## Copyright 2013 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.
|
||||
##
|
||||
###############################################################################
|
||||
|
||||
@@ -17,27 +17,31 @@
|
||||
###############################################################################
|
||||
|
||||
import asyncio
|
||||
from asyncio import Protocol
|
||||
|
||||
from autobahn.websocket2 import protocol
|
||||
|
||||
|
||||
class AdapterProtocol(Protocol):
|
||||
|
||||
class WebSocketServerProtocol(asyncio.Protocol, protocol.WebSocketServerProtocol):
|
||||
|
||||
def connection_made(self, transport):
|
||||
self.transport = transport
|
||||
peername = transport.get_extra_info('peername')
|
||||
print('connection from {}'.format(peername))
|
||||
self._protocol.transport = transport
|
||||
|
||||
def data_received(self, data):
|
||||
self._protocol.data_received(data)
|
||||
self._onData(data)
|
||||
#print('data received: {}'.format(data.decode()))
|
||||
#self.transport.write(data)
|
||||
#self.transport.close()
|
||||
|
||||
|
||||
class AdapterFactory:
|
||||
|
||||
def __init__(self, factory, loop = None):
|
||||
self._factory = factory
|
||||
class WebSocketServerFactory(protocol.WebSocketServerFactory):
|
||||
|
||||
protocol = WebSocketServerProtocol
|
||||
|
||||
def __init__(self, loop = None):
|
||||
|
||||
if loop is None:
|
||||
loop = asyncio.get_event_loop()
|
||||
@@ -45,8 +49,6 @@ class AdapterFactory:
|
||||
|
||||
|
||||
def __call__(self):
|
||||
proto = AdapterProtocol()
|
||||
proto = self.protocol()
|
||||
proto.factory = self
|
||||
proto._protocol = self._factory()
|
||||
print("asyncio protocol created")
|
||||
return proto
|
||||
18
autobahn/autobahn/twisted/__init__.py
Normal file
18
autobahn/autobahn/twisted/__init__.py
Normal file
@@ -0,0 +1,18 @@
|
||||
###############################################################################
|
||||
##
|
||||
## Copyright 2013 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.
|
||||
##
|
||||
###############################################################################
|
||||
|
||||
@@ -17,27 +17,31 @@
|
||||
###############################################################################
|
||||
|
||||
from __future__ import absolute_import
|
||||
from twisted.internet.protocol import Protocol, Factory
|
||||
import twisted.internet.protocol
|
||||
|
||||
from autobahn.websocket2 import protocol
|
||||
|
||||
|
||||
class AdapterProtocol(Protocol):
|
||||
|
||||
class WebSocketServerProtocol(twisted.internet.protocol.Protocol, protocol.WebSocketServerProtocol):
|
||||
|
||||
def connectionMade(self):
|
||||
peername = str(self.transport.getPeer())
|
||||
print('connection from {}'.format(peername))
|
||||
self._protocol.transport = self.transport
|
||||
|
||||
def dataReceived(self, data):
|
||||
self._protocol.data_received(data)
|
||||
self._onData(data)
|
||||
#print('data received: {}'.format(data.decode()))
|
||||
#self.transport.write(data)
|
||||
#self.transport.loseConnection()
|
||||
|
||||
|
||||
class AdapterFactory(Factory):
|
||||
|
||||
def __init__(self, factory, reactor = None):
|
||||
self._factory = factory
|
||||
class WebSocketServerFactory(twisted.internet.protocol.Factory, protocol.WebSocketServerFactory):
|
||||
|
||||
protocol = WebSocketServerProtocol
|
||||
|
||||
def __init__(self, reactor = None):
|
||||
|
||||
## lazy import to avoid reactor install upon module import
|
||||
if reactor is None:
|
||||
@@ -46,8 +50,6 @@ class AdapterFactory(Factory):
|
||||
|
||||
|
||||
def buildProtocol(self, addr):
|
||||
proto = AdapterProtocol()
|
||||
proto = self.protocol()
|
||||
proto.factory = self
|
||||
proto._protocol = self._factory()
|
||||
print("twisted protocol created")
|
||||
return proto
|
||||
@@ -18,7 +18,7 @@
|
||||
|
||||
class WebSocketServerProtocol:
|
||||
|
||||
def data_received(self, data):
|
||||
def _onData(self, data):
|
||||
print('data received: {}'.format(data.decode()))
|
||||
self.onMessage(data, False)
|
||||
|
||||
@@ -31,9 +31,4 @@ class WebSocketServerProtocol:
|
||||
|
||||
class WebSocketServerFactory:
|
||||
|
||||
protocol = WebSocketServerProtocol
|
||||
|
||||
def __call__(self):
|
||||
proto = self.protocol()
|
||||
proto.factory = self
|
||||
return proto
|
||||
pass
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
server1_asyncio:
|
||||
PYTHONPATH=../../autobahn python3 server.py asyncio
|
||||
PYTHONPATH=../../autobahn python3 server_asyncio.py
|
||||
|
||||
server1_twisted:
|
||||
PYTHONPATH=../../autobahn python server.py twisted
|
||||
PYTHONPATH=../../autobahn python server_twisted.py
|
||||
|
||||
client1_asyncio:
|
||||
PYTHONPATH=../../autobahn python3 client.py
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
###############################################################################
|
||||
##
|
||||
## Copyright 2011-2013 Tavendo GmbH
|
||||
## Copyright 2013 Tavendo GmbH
|
||||
##
|
||||
## Licensed under the Apache License, Version 2.0 (the "License");
|
||||
## you may not use this file except in compliance with the License.
|
||||
@@ -16,7 +16,9 @@
|
||||
##
|
||||
###############################################################################
|
||||
|
||||
from autobahn.websocket2.protocol import WebSocketServerProtocol, WebSocketServerFactory
|
||||
from autobahn.asyncio.websocket import WebSocketServerProtocol, \
|
||||
WebSocketServerFactory
|
||||
|
||||
|
||||
|
||||
class MyServerProtocol(WebSocketServerProtocol):
|
||||
@@ -25,6 +27,9 @@ class MyServerProtocol(WebSocketServerProtocol):
|
||||
print("message received")
|
||||
self.sendMessage(payload)
|
||||
|
||||
def onConnect(self, connectionRequest):
|
||||
return None
|
||||
|
||||
|
||||
class MyServerFactory(WebSocketServerFactory):
|
||||
|
||||
@@ -32,14 +37,16 @@ class MyServerFactory(WebSocketServerFactory):
|
||||
|
||||
|
||||
|
||||
def run_test_asyncio(factory):
|
||||
if __name__ == '__main__':
|
||||
|
||||
import asyncio
|
||||
|
||||
from autobahn.asyncio import AdapterFactory
|
||||
factory = MyServerFactory()
|
||||
|
||||
loop = asyncio.get_event_loop()
|
||||
coro = loop.create_server(AdapterFactory(factory), '127.0.0.1', 8888)
|
||||
coro = loop.create_server(factory, '127.0.0.1', 8888)
|
||||
server = loop.run_until_complete(coro)
|
||||
|
||||
print('serving on {}'.format(server.sockets[0].getsockname()))
|
||||
|
||||
try:
|
||||
@@ -49,27 +56,3 @@ def run_test_asyncio(factory):
|
||||
finally:
|
||||
server.close()
|
||||
loop.close()
|
||||
|
||||
|
||||
def run_test_twisted(factory):
|
||||
from twisted.internet.endpoints import TCP4ServerEndpoint
|
||||
from twisted.internet import reactor
|
||||
|
||||
from autobahn.twisted import AdapterFactory
|
||||
|
||||
endpoint = TCP4ServerEndpoint(reactor, 8888)
|
||||
endpoint.listen(AdapterFactory(factory))
|
||||
reactor.run()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
import sys
|
||||
|
||||
factory = MyServerFactory()
|
||||
|
||||
if sys.argv[1] == "asyncio":
|
||||
run_test_asyncio(factory)
|
||||
elif sys.argv[1] == "twisted":
|
||||
run_test_twisted(factory)
|
||||
else:
|
||||
raise Exception("no such variant")
|
||||
49
examples/websocket2/server_twisted.py
Normal file
49
examples/websocket2/server_twisted.py
Normal file
@@ -0,0 +1,49 @@
|
||||
###############################################################################
|
||||
##
|
||||
## Copyright 2013 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.twisted.websocket import WebSocketServerProtocol, \
|
||||
WebSocketServerFactory
|
||||
|
||||
|
||||
|
||||
class MyServerProtocol(WebSocketServerProtocol):
|
||||
|
||||
def onMessage(self, payload, isBinary):
|
||||
print("message received")
|
||||
self.sendMessage(payload)
|
||||
|
||||
def onConnect(self, connectionRequest):
|
||||
return None
|
||||
|
||||
|
||||
class MyServerFactory(WebSocketServerFactory):
|
||||
|
||||
protocol = MyServerProtocol
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
from twisted.internet.endpoints import TCP4ServerEndpoint
|
||||
from twisted.internet import reactor
|
||||
|
||||
factory = MyServerFactory()
|
||||
|
||||
endpoint = TCP4ServerEndpoint(reactor, 8888)
|
||||
endpoint.listen(factory)
|
||||
reactor.run()
|
||||
Reference in New Issue
Block a user