Openshift 3.X with Websockets

2019-08-02 18:01发布

问题:

I'm trying to host a websocket-application op Openshift 3, but I have running into some issues.

Code at the backend:

templatePath = os.path.join(os.path.dirname(__file__), "templates")
staticPath = os.path.join(os.path.dirname(__file__), "static")

settings = {
    "template_path": templatePath,
    "static_path": staticPath,
    "debug" : True
}

application = web.Application([
    (r'/ws', WSHandler),
    (r'/', MainHandler, {"staticFilesPath":staticPath}),
    (r"/(.*)", tornado.web.StaticFileHandler, {'path': staticPath}),
],**settings)

class WSHandler(tornado.websocket.WebSocketHandler):
    def initialize(self, messageHandler):
        print 'Initializing MessageHandler'
        self.messageHandler = messageHandler

    def check_origin(self,origin):
        return True

    def open(self):
        print 'Connection received'

    def on_message(self, message):
        message = simplejson.loads(message)
        print 'received message of type "' + message['type'] + '"'

    outputMessage = {'type': 'Hello', 'data': 'World'}
    self.sendMessage(outputMessage)

def on_close(self):
    print 'Connection closed'

class MainHandler(tornado.web.RequestHandler):

    def initialize(self, staticFilesPath):
        self.staticFilesPath = staticFilesPath

    def get(self):
        print 'New connection'    
        webClientHtml = os.path.join(self.staticFilesPath,'index.html')
        loader = tornado.template.Loader(".")
        self.write(loader.load(webClientHtml).generate())

At the frontend:

var hostname = window.document.location.hostname;
var host = "ws://" + hostname + ":8000/ws";

var ws = new WebSocket(host);

This leads to ERR_CONNECTION_TIMED_OUT. I know the port 8000 had to be specified in Openshift 2. The documentation I could find about websocket-ports op Openshift 3 seems to suggest this is no longer necessary, but I can't figure out what to use. I have tried 80 and not specifying a port, but neither works.

Does anybody know what I'm doing wrong?