首页 > 解决方案 > Elixir Pheonix Channels (FunctionClauseError) 在 ProjectName.ModuleName.handle_in/3 中没有函数子句匹配

问题描述

通过 Typescript 连接到 Elixir 频道时,出现此错误

( FunctionClauseError) 没有匹配的函数子句ProjectName.ModuleName.handle_in/3

  1. 为什么会发生错误?
  2. 我该如何解决?

这是我的代码。

打字稿 chatView.tsx

import React, { Component } from 'react'
import { Socket} from 'phoenix'

type MyProps = {  };
type MyState = { message: string  };

export class Chat extends Component <MyProps, MyState> {

    static readonly sockets = new Socket("ws://127.0.0.1:4000/socket");
    static channel = Chat.sockets.channel("groups_forums:lobby", {})

    constructor(props: any) {
        super(props);

        Chat.sockets.connect()

        //bind
        this.handleChange = this.handleChange.bind(this);
        this.handleClick = this.handleClick.bind(this);
        this.handleLoad = this.handleLoad.bind(this);
        this.keyPress = this.keyPress.bind(this);

    }


    componentDidMount() {
        window.addEventListener('load', this.handleLoad);
    }

    handleLoad() {
        console.log("component loaded");

        Chat.channel.join()
            .receive("ok", (resp: any) => { console.log("Joined successfully", resp) })
            .receive("error", (resp: any) => { console.log("Unable to join", resp) })
            .receive("ignore", (resp: any) => {console.log("auth error", resp)})

        Chat.channel.onClose((close: any) => { console.log("closing bye  ", close) });


       Chat.channel.on("new:msg", msg => {
            scrollTo(0, document.body.scrollHeight)
          })

    }


    handleChange(e:any) {
        this.setState({ message: e.target.value });
   }

    keyPress(e:any){
        if (e.keyCode == 13) {
         Chat.channel.push("new:msg",{message: this.state.message},2000)   
        }
      }

    handleClick(e: any) {
        e.preventDefault();
       Chat.channel.push("new:msg", {message: this.state.message})
    }


    render() {
        return (
            <div style={{ position: "absolute", bottom: 0 }}>
                <form>
                    <p className="form-group row">
                        <input style={{ marginLeft: 20, width: 200 }} defaultValue={''} onKeyPress={this.keyPress} onChange={ this.handleChange } id="message" type="text"></input>

                        <input style={{ marginLeft: 20, marginRight: 20 }} type="button" value="send" id="button" onClick={this.handleClick}></input>
                    </p>
                </form>
            </div>
        )
    }
}

export default Chat

灵丹妙药 phoenix user_socket.ex

channel "groups_forums:lobby", ChatSample.GroupsForumsChannel

Module ChatSample.GroupsForumsChannelingroups_forums_channel.ex是默认的自动生成模板。

我想要完成的任务的简要总结是,应该将用户消息广播给连接到频道的所有成员。

标签: reactjstypescriptwebsocketelixirphoenix-framework

解决方案


错误出现在我的长生不老药代码中,group_forums_channel.ex我应该包含此代码来处理传入的消息

  def handle_in("new_msg", %{"body" => body}, socket) do
    broadcast socket, "new_msg", %{body: body}
    {:noreply, socket}
  end


推荐阅读