带Websockets的Openshift 3.X

时间:2017-05-30 19:17:54

标签: websocket openshift

我试图托管一个websocket-application op Openshift 3,但我遇到了一些问题。

后端的代码:

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())

在前端:

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

var ws = new WebSocket(host);

这会导致ERR_CONNECTION_TIMED_OUT。我知道必须在Openshift 2中指定端口8000.我可以找到关于websocket-ports操作的文档操作Openshift 3似乎表明这不再是必要的,但我无法弄清楚要使用什么。我已经尝试过80而没有指定端口,但都不起作用。

有人知道我做错了吗?

0 个答案:

没有答案