【问题标题】:Elixir Pheonix Channels (FunctionClauseError) no function clause matching in ProjectName.ModuleName.handle_in/3Elixir Pheonix Channels (FunctionClauseError) 在 ProjectName.ModuleName.handle_in/3 中没有函数子句匹配
【发布时间】:2019-08-06 03:58:30
【问题描述】:

通过 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

groups_forums_channel.ex 中的模块ChatSample.GroupsForumsChannel 是默认的自动生成模板。

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

【问题讨论】:

  • 能否请您从 TS 的这堵墙中提取引起错误的部分(连接?)?
  • 谢谢,终于找到错误了。它在我的 Elixir 代码中。

标签: reactjs typescript websocket elixir phoenix-framework


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2019-06-15
    • 2021-01-22
    • 2017-11-30
    • 2019-06-27
    • 1970-01-01
    • 2017-11-26
    • 2023-03-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多