Openshift 3.X with Websockets

本秂侑毒 提交于 2019-12-24 06:26:10

问题


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?

来源:https://stackoverflow.com/questions/44270244/openshift-3-x-with-websockets

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!