javascript - phoenix.js: WebSocket connection to ws:localhost:4000socket failed - Stack Overflow

I get this error in the Chrome console on my PhoenixReact app—but, strangely, not in incognito mode. E

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 badges
Add a ment  | 

1 Answer 1

Reset to default 8

I'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条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信