带WebSocket的Openshift 3.X
我正在尝试托管websocket应用程序OPOpenShift 3,但遇到了一些问题 后端的代码:带WebSocket的Openshift 3.X,websocket,openshift,Websocket,Openshift,我正在尝试托管websocket应用程序OPOpenShift 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": s
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);
这会导致错误连接超时。我知道端口8000必须在Openshift 2中指定。我能找到的关于websocket端口OPOpenShift 3的文档似乎表明这不再是必要的,但我不知道该使用什么。我尝试了80,但没有指定端口,但两者都不起作用
有人知道我做错了什么吗?如果您使用的是Python S2I builder for OpenShift 3,则需要您的应用程序代码在内部使用端口8080,而不是8000。一个端口处理正常HTTP通信和web套接字请求。从外部看,您的web应用程序在普通端口80上是可见的,因此不要让客户端使用端口8000或8080作为侦听端口;谢谢这帮了我,谢谢!我建议您将此添加为问题的答案(为了更好的可见性),如果您使用的是Python S2I builder for OpenShift 3,则需要您的应用程序代码在内部使用端口8080,而不是8000。一个端口处理正常HTTP通信和web套接字请求。从外部看,您的web应用程序在普通端口80上是可见的,因此不要让客户端使用端口8000或8080作为侦听端口;谢谢这帮了我,谢谢!我建议您将此添加为问题的答案(以提高可见性)