Answer the question
In order to leave comments, you need to log in
Why is socket.io not accepting data?
Hello, there was a need to use go-socket.io.
Everything seems to be done correctly, but the data does not reach.
Route
r.Handle("/socket.io/", sio.CreateSocketIoServer())
func CreateSocketIoServer() *socketio.Server {
server, err := socketio.NewServer(nil)
if err != nil {
log.Fatal(err)
}
server.On("connection", func(so socketio.Socket) {
log.Println("on connection")
so.Join("chat")
so.On("send", func(msg string) {
log.Println(msg)
})
so.On("disconnection", func() {
log.Println("on disconnect")
})
})
server.On("error", func(so socketio.Socket, err error) {
log.Println("error:", err)
})
return server
}
<div class="chat-foot">
<form id="SendMs" action="#" method="post">
<input type="text" id="msg" name="text" placeholder="Введите сообщение" />
</form>
</div>
var socket = io('');
$('#SendMs').submit(function() {
var msgField = $('#msg'),
data = { msg: msgField.val() };
socket.emit('send', data);
console.log(data);
});
connection
Object { msg="fggfhhfjjtfhk"}
2016/07/29 05:50:32 on disconnect
2016/07/29 05:50:33 on connection
Answer the question
In order to leave comments, you need to log in
damn, and the syntax.
Yuzai https://github.com/gorilla/websocket
Because when sending data, the page is reloaded by defort. When the page is reloaded, the socket terminates the current connection and opens a new one with a new soccketId. Those. the data that you sent from the form from the front left, but because the server connection was broken due to a reboot. Accordingly, your data has gone nowhere.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question