将远程客户端连接到Phoenix通道

时间:2017-12-16 22:43:19

标签: reactjs websocket phoenix phoenix-channels

我试图将我的前端与Phoenix 1.3(localhost:4000)分开,并且我在localhost:3000上运行React。

我不能为我的生活连接到凤凰城。我收到错误:

WebSocket connection to 'ws://localhost:4000/socket/websocket?token=undefined&vsn=2.0.0' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED

我使用以下客户端:

let socket = new Socket("ws://localhost:4000/socket", {params: {token: window.userToken}})

    socket.connect();

    // Now that you are connected, you can join channels with a topic:
    let channel = socket.channel("coin:lobby", {});
    channel
      .join()
      .receive("ok", resp => {
        console.log("Joined successfully", resp);
      })
      .receive("error", resp => {
        console.log("Unable to join", resp);
      });

我可以设法在终端使用wsta连接,我收到了很好的回复。我认为这可能是某种CORS问题,但是我已经实现了cors_plug灵丹妙药,我仍然遇到了这个问题。有什么建议吗?

1 个答案:

答案 0 :(得分:0)

您必须首先安装phoenix客户端

npm install phoenix --save

import {Socket} from "phoenix"; in your component 

let socket = new Socket("ws://localhost:4000/socket");

socket.connect();
let channel = socket.channel("chat_room:lobby", {});
console.log(channel);

我也遇到过同样的问题,并用相同的代码解决了。

请引用此链接enter link description here