Makes websocket work with qunit testing, some code changes as part of
refactoring for testing.