I get this error in the Chrome console on my Phoenix/React app—but, strangely, not in incognito mode. Everything is pointed to the same /socket
endpoint.
WebSocket connection to 'ws://localhost:4000/socket/websocket?token=blah&vsn=1.0.0' failed: Error during WebSocket handshake: Unexpected response code: 400
Various sources say it's about the transport layer, which makes a bit of sense since this is what's highlighted in the console where the error is:
this.conn = new this.transport(this.endPointURL());
The React app finds the endpoint like so:
const API_URL = 'http://localhost:4000/api';
const WEBSOCKET_URL = API_URL.replace(/(https|http)/, 'ws').replace('/api', '');
function connectToSocket(dispatch) {
const token = JSON.parse(localStorage.getItem('token'));
const socket = new Socket(`${WEBSOCKET_URL}/socket`, {
params: { token },
});
socket.connect();
dispatch({ type: 'SOCKET_CONNECTED', socket });
}
NB: Amending that line to const socket = new Socket('${WEBSOCKET_URL}/socket/websocket'
just makes the error read .../socket/websocket/websocket?...
.
The endpoint.ex
is pretty standard:
socket "/socket", App.UserSocket
None of this points to why the app would work in incognito and load all these errors otherwise, though.
I get this error in the Chrome console on my Phoenix/React app—but, strangely, not in incognito mode. Everything is pointed to the same /socket
endpoint.
WebSocket connection to 'ws://localhost:4000/socket/websocket?token=blah&vsn=1.0.0' failed: Error during WebSocket handshake: Unexpected response code: 400
Various sources say it's about the transport layer, which makes a bit of sense since this is what's highlighted in the console where the error is:
this.conn = new this.transport(this.endPointURL());
The React app finds the endpoint like so:
const API_URL = 'http://localhost:4000/api';
const WEBSOCKET_URL = API_URL.replace(/(https|http)/, 'ws').replace('/api', '');
function connectToSocket(dispatch) {
const token = JSON.parse(localStorage.getItem('token'));
const socket = new Socket(`${WEBSOCKET_URL}/socket`, {
params: { token },
});
socket.connect();
dispatch({ type: 'SOCKET_CONNECTED', socket });
}
NB: Amending that line to const socket = new Socket('${WEBSOCKET_URL}/socket/websocket'
just makes the error read .../socket/websocket/websocket?...
.
The endpoint.ex
is pretty standard:
socket "/socket", App.UserSocket
None of this points to why the app would work in incognito and load all these errors otherwise, though.
Share Improve this question asked Jun 13, 2017 at 21:59 t56kt56k 7,03110 gold badges58 silver badges121 bronze badges1 Answer
Reset to default 8I'll post how I fixed this because it might help someone else, although I'm not really certain why it's the case. Just change localhost
in both the front and the back to the IP address:
In config.ex
:
config :app, App.Endpoint,
url: [host: "127.0.0.1"]...
In your front end:
const API_URL = 'http://127.0.0.1:4000/api';
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744889974a4599350.html
评论列表(0条)