reactjs - Elixir Pheonix Channels (FunctionClauseError) 在 ProjectName.ModuleName.handle_in/3 中没有函数子句匹配
问题描述
通过 Typescript 连接到 Elixir 频道时,出现此错误
(
FunctionClauseError
) 没有匹配的函数子句ProjectName.ModuleName.handle_in/3
- 为什么会发生错误?
- 我该如何解决?
这是我的代码。
打字稿 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.GroupsForumsChannel
ingroups_forums_channel.ex
是默认的自动生成模板。
我想要完成的任务的简要总结是,应该将用户消息广播给连接到频道的所有成员。
解决方案
错误出现在我的长生不老药代码中,group_forums_channel.ex
我应该包含此代码来处理传入的消息
def handle_in("new_msg", %{"body" => body}, socket) do
broadcast socket, "new_msg", %{body: body}
{:noreply, socket}
end
推荐阅读
- node.js - 从 API 返回数据的最佳方式
- javascript - 为什么我的边距定义在 iPad 上占满宽度
- android - 在 c++ 和 opengl 中实现撤消和重做功能的问题
- swift - SCNNode中的Orientation和Rotation有什么区别
- algorithm - 将 0 和 1 的序列相加并将结果写入基数 2 的高效算法
- performance - TTFB(Time-To-First-Byte)计算由什么组成,以及如何查看各个组件的时序?
- vue.js - 如何将 Vue Webpack-simple 应用程序部署到 Netlify?
- c++ - 重写虚函数和继承
- neo4j - Neo4j:将不同节点链接到一个节点
- arrays - Angular2:如果项目包含在其他其他数组中,则过滤数组