Q
Q
qwex2016-07-29 09:16:46
go
qwex, 2016-07-29 09:16:46

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())

CreateSocketIoServer() function
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
}

The form
<div class="chat-foot">
                    <form id="SendMs" action="#" method="post">
                        <input type="text" id="msg" name="text" placeholder="Введите сообщение" />
                    </form>
                </div>

Connected
And myself js
var socket = io('');
        $('#SendMs').submit(function() {
           var msgField = $('#msg'),
           data = { msg: msgField.val() };
           socket.emit('send', data);
          console.log(data);
        });

When entering the chat page, the application console displays
connection

When submitting the form, the page is reloaded and displayed in console.log

Object { msg="fggfhhfjjtfhk"}

In the application console - empty and there is no data that I sent and due to the fact that the page was refreshed in the console

2016/07/29 05:50:32 on disconnect
2016/07/29 05:50:33 on connection

Can you please tell me why the page is being updated and the data is not coming?
Thanks in advance.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vladimir Grabko, 2016-07-29
@VGrabko

damn, and the syntax.
Yuzai https://github.com/gorilla/websocket

A
Alexey Dodoka, 2019-11-01
@alex-dodoka

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 question

Ask a Question

731 491 924 answers to any question