tavendo/AutobahnPython — pull request #196 by kousu (2014-03-16 17:12:47+00:00)
Title: Nullports

We are running our test server on port 8080. Because of this, we've been instantiating Autobahn with
```
WebSocketServerFactory("ws://localhost:8080")
```
but I don't necessarily know that I'm using "ws" or "localhost" or "8080" when I create that object!! I did some experimenting and wrote up a minimal test case, showing three ways of calling WebSocketServerFactory that work and one--the one that I care about--not working:

```
"""
minimal test case for removing dependency on WebSocketServerProtocol.setSessionParams()

"""

import sys

from twisted.internet import reactor
from twisted.internet import task
from twisted.python import log
from twisted.web.server import Site
from twisted.web.static import File
from twisted.internet.defer import inlineCallbacks

from autobahn.twisted.websocket import WebSocketServerFactory, WebSocketServerProtocol
from autobahn.twisted.resource import WebSocketResource

class EchoProtocol(WebSocketServerProtocol):
def onConnect(self, request):
print("Client connecting: {}".format(request.peer))

def onOpen(self):
print("WebSocket connection open.")

def onMessage(self, payload, isBinary):
if isBinary:
print("Binary message received: {} bytes".format(len(payload)))
else:
print("Text message received: |{}|".format(payload.decode('utf8')))

self.sendMessage(payload*2, isBinary)

def onClose(self, wasClean, code, reason):
print("WebSocket connection closed: {}".format(reason))

if __name__ == '__main__':
log.startLogging(sys.stdout)

# all these three are equivalent under AutobahnPython 0.8.5
#ws_echo_endpoint = WebSocketServerFactory("ws://localhost:8080", debug=True, debugCodePaths=True)
#ws_echo_endpoint = WebSocketServerFactory("ws://:8080", debug=True, debugCodePaths=True)
#ws_echo_endpoint = WebSocketServerFactory(externalPort=8080, debug=True, debugCodePaths=True)
# this one is not:
ws_echo_endpoint = WebSocketServerFactory(debug=True, debugCodePaths=True)

ws_echo_endpoint.protocol = EchoProtocol

root = File(".")
ws_echo_resource = WebSocketResource(ws_echo_endpoint)
root.putChild("echo", ws_echo_resource)

site = Site(root)
reactor.listenTCP(8080, site)

print "Now open http://127.0.0.1:8080 in your browser"
reactor.run()

```

```
/* autobah.js
* runs under nodejs
*/
WebSocket = require("ws");
ws = new WebSocket("ws://localhost:8080/echo");
ws.onmessage = function(evt) {
console.log("received", evt.data);
}

ws.onopen = function(evt) {
setInterval(function() { ws.send("fancy free"); }, 4000);
}
```

Is it really necessary to have all the checks in `protocol.py`? WebSocketServerFactory recently said "You must set the URL either here or by calling setSessionParams() before starting the Factory", and I find that onerous. The whole reason I'm using Twisted is so that I can put up endpoints willy-nilly. Is the oner part of the websocket spec or just something that seemed like a good idea? This corner makes an otherwise very useful library inflexible.