问题
I am making a multiplayer game using Socket.io, Node.js, and Openshift. It was working for a while, but I made a few small changes and now something unrelated to what I changed has broken. When a second player tries to join the game, it boots everyone out, showing a 503 error for anyone else who goes on the site. For the second player, I get a lot of console errors including
WebSocket connection to 'ws://avalon-evao.rhcloud.com/socket.io/?EIO=3&transport=websocket&sid=d5smykffy4EWHxgIAAAC' failed: Error during WebSocket handshake: Unexpected response code: 400
http://avalon-evao.rhcloud.com/socket.io/?EIO=3&transport=polling&t=1421020862973-4&sid=d5smykffy4EWHxgIAAAC Failed to load resource: the server responded with a status of 502 (Bad Gateway)
http://avalon-evao.rhcloud.com/socket.io/?EIO=3&transport=polling&t=1421020865831-6&sid=d5smykffy4EWHxgIAAAC Failed to load resource: the server responded with a status of 503 (Service Unavailable)
socket.io-1.2.0.js:2 WebSocket connection to 'ws://avalon-evao.rhcloud.com/socket.io/?EIO=3&transport=websocket&sid=c-fwcJaMkp_9rl5CAAAA' failed: WebSocket is closed before the connection is established.
It works perfectly fine in localhost, but the real site is broken.
Here's what I've tried:
- Using HTTPS. I still somehow get the 502 error.
- Restarting the app using
rhc app restart
- Restarting the app using
rhc app force-stop rhc app start
- Checking the memory limit. I haven't hit it.
- Connecting to websockets securely using the
{ secure: true }
option
What is causing these errors and how can I fix them?
回答1:
Try:
ws://...rhcloud.com:8000/socket.io/...
wss://...rhcloud.com:8443/socket.io/...
https://help.openshift.com/hc/en-us/articles/203263674-What-external-ports-are-available-on-OpenShift
回答2:
It turns out I was looking at the logs wrong. When I figured out how to read the logs for rhc, I found an error in the database. I had changed the structure of the collections without dropping the database, so there were indexes to keys that no longer existed. Dropped the database, and the app works fine now.
来源:https://stackoverflow.com/questions/27894058/400-502-503-errors-with-websockets