2

プロジェクトで色の配布に Phoenix Channels を使用しましたが、次のようなエラーが発生し続けます。

[debug] Duplicate channel join for topic "colors:*" in EveryColor.ColorSocket. Closing existing channel for new join

複製方法:

  1. make install && make build
  2. 混合phoenix.server
  3. ウェブページを一度読み込むと、すべてが機能します
  4. リロードと新しい色のボタンが機能しなくなり、上記のメッセージがコンソールに表示されます..

コード:

バックエンド

defmodule EveryColor.DistributionChannel do
  use Phoenix.Channel

  alias EveryColor.Distributor

  def join("colors:"<>_ , _message, socket) do
    #send(self, :after_join)
    {:ok, %{color: Distributor.random_color, counter: Distributor.get_counter+1} ,socket}
  end

  def handle_info(:after_join, socket) do
    broadcast_counter socket
    push socket, "color_generated", %{color: Distributor.random_color}
    {:noreply, socket}
  end

  def handle_in("get", _payload, socket) do
    broadcast_counter socket
    {:reply, {:ok, %{color: Distributor.random_color}}, socket}
  end

  def broadcast_counter(socket) do
    broadcast socket, "counter_bump", %{counter: Distributor.get_counter+1}
  end

end

フロント:

import { Socket } from 'phoenix-socket';
import { store } from './App.jsx';

let channel;

export default function connectToSocket(channelName) {

  const logs = process.env.NODE_ENV === 'development'
        ? { logger: ((kind, msg, data) => { console.log(`kind: ${kind}: msg: ${msg}`, data); })}
        : { };

  const socket = new Socket(`ws:localhost:4000/socket`, logs);

  socket.connect();

  socket.onOpen( e => console.log('Socket onOpen: ', e));
  socket.onError( e => console.log('Socket onError: ', e));
  socket.onClose( e => console.log('Socket onClose: ', e));

  channel = socket.channel(channelName);
  channel.join(5000)
    .receive('ok', data => {
      store.dispatch({type: 'NEXT_COLOR', color: data.color });
      store.dispatch({type: 'UPDATE_COUNTER', counter: data.counter });
    })
    .receive('error', err => {
      console.log(err);
    });

  channel.on('color_generated', payload => {
    console.log(payload.color);
    store.dispatch({type: 'NEXT_COLOR', color: payload.color });
  });

  channel.on('counter_bump', payload => {
    store.dispatch({type: 'UPDATE_COUNTER', counter: payload.counter });
  });

}

export function dispatchSocketMessage(message) {
  return new Promise((resolve, reject) => {
    channel.push(message)
      .receive('ok', resolve)
      .receive('err', reject);
  });
}

リポジトリ:

  1. フロント
  2. バックエンド
4

0 に答える 0