When connecting to Elixir channels via Typescript, I get this error
(
FunctionClauseError
) no function clause matching inProjectName.ModuleName.handle_in/3
Here is my code.
typescript 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
elixir phoenix user_socket.ex
channel "groups_forums:lobby", ChatSample.GroupsForumsChannel
Module ChatSample.GroupsForumsChannel
in groups_forums_channel.ex
is the default auto-generated template.
A brief summary on the task I want to accomplish is that the user message should be broadcast to all members connected to the channel.
The error was in my elixir code,the group_forums_channel.ex
i should have included this code to handle the incoming message
def handle_in("new_msg", %{"body" => body}, socket) do
broadcast socket, "new_msg", %{body: body}
{:noreply, socket}
end
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.