socket.io-client-swift icon indicating copy to clipboard operation
socket.io-client-swift copied to clipboard

Emit doesn't work

Open WGPavell opened this issue 4 years ago • 0 comments

I try to emit with this client but in most cases server doesn't receive it. Client logs:

2021-09-22 17:43:07.788557+0300 My App[46226:8969192] LOG SocketIOClient{/}: Adding handler for event: connect
2021-09-22 17:43:07.788778+0300 My App[46226:8969192] LOG SocketIOClient{/}: Adding handler for event: personal_chat_send_message
2021-09-22 17:43:07.788958+0300 My App[46226:8969192] LOG SocketIOClient{/}: Handling event: statusChange with data: [connecting, 2]
2021-09-22 17:43:07.789132+0300 My App[46226:8969192] LOG SocketIOClient{/}: Joining namespace /
2021-09-22 17:43:07.789305+0300 My App[46226:8969192] LOG SocketManager: Tried connecting socket when engine isn't open. Connecting
2021-09-22 17:43:07.789514+0300 My App[46226:8969192] LOG SocketManager: Adding engine
2021-09-22 17:43:07.789837+0300 My App[46226:8969985] LOG SocketEngine: Starting engine. Server: https://myserver
2021-09-22 17:43:07.789993+0300 My App[46226:8969985] LOG SocketEngine: Handshaking
2021-09-22 17:43:07.817286+0300 My App[46226:8969192] LOG SocketIOClient{/}: Handling event: websocketUpgrade with data: [["Upgrade": "websocket", "Sec-WebSocket-Accept": "jT5+AshZ0qfooIqPP5uMjN27daM=", "Connection": "Upgrade"]]
2021-09-22 17:43:07.817286+0300 My App[46226:8969985] LOG SocketEngine: Got message: 0{"sid":"Za4h8by7MRFk2P9uAAAE","upgrades":[],"pingInterval":25000,"pingTimeout":5000}
2021-09-22 17:43:07.817544+0300 My App[46226:8969192] LOG SocketIOClient{/}: Handling event: ping with data: []
2021-09-22 17:43:07.817545+0300 My App[46226:8969985] LOG SocketEngine: Writing ws:  has data: false
2021-09-22 17:43:07.817655+0300 My App[46226:8969192] LOG SocketManager: Engine opened Connect
2021-09-22 17:43:07.817688+0300 My App[46226:8969985] LOG SocketEngineWebSocket: Sending ws:  as type: 2
2021-09-22 17:43:07.817781+0300 My App[46226:8969192] LOG SocketIOClient{/}: Socket connected
2021-09-22 17:43:07.817881+0300 My App[46226:8969192] LOG SocketIOClient{/}: Handling event: statusChange with data: [connected, 3]
2021-09-22 17:43:07.817983+0300 My App[46226:8969192] LOG SocketIOClient{/}: Handling event: connect with data: ["/"]
connected
2021-09-22 17:43:07.818123+0300 My App[46226:8969192] LOG SocketIOClient{/}: Adding once handler for event: pong
2021-09-22 17:43:07.824749+0300 My App[46226:8969495] LOG SocketEngine: Got message: 3
2021-09-22 17:43:07.825082+0300 My App[46226:8969192] LOG SocketIOClient{/}: Handling event: pong with data: []
2021-09-22 17:43:07.825273+0300 My App[46226:8969192] LOG SocketIOClient{/}: Removing handler with id: 14D80209-2150-4512-9619-D3CEAB337E57
2021-09-22 17:43:07.825506+0300 My App[46226:8969192] LOG SocketIOClient{/}: Emitting: 2["personal_chat","fdsfds"], Ack: false
2021-09-22 17:43:07.825741+0300 My App[46226:8969495] LOG SocketEngine: Writing ws: 2["personal_chat","fdsfds"] has data: false
2021-09-22 17:43:07.825908+0300 My App[46226:8969495] LOG SocketEngineWebSocket: Sending ws: 2["personal_chat","fdsfds"] as type: 4
emitted
2021-09-22 17:43:07.870880+0300 My App[46226:8969985] LOG SocketEngine: Got message: 40
2021-09-22 17:43:07.871108+0300 My App[46226:8969192] LOG SocketParser: Parsing 0
2021-09-22 17:43:07.871292+0300 My App[46226:8969192] LOG SocketParser: Decoded packet as: SocketPacket {type: 0; data: []; id: -1; placeholders: 0; nsp: /}

I connect to server with following code:

self?.socketManager.setConfigs([.version(.two), .reconnects(true), .extraHeaders(["billing-token": token, "User-Agent": userAgent]), .forceNew(true), .compress, .forceWebsockets(true), .log(true)])
let socket = self?.socketManager.defaultSocket
socket?.on("connect", callback: { data, ack in
    print("connected")
    self?.socketManager.handleQueue.async {
        socket?.once(clientEvent: .pong, callback: { data, ack  in
            socket?.emit("personal_chat", "fdsfds") {
                print("emitted")
            }
        })
    }
})
socket?.on("personal_chat_send_message", callback: { data, ack in
    debugPrint(data)
})
socket?.connect()

In logs connected and emitted are printed. Early I try it without handleQueue, pong event, data (just only socket.emit with event name) and result was same. I try to add delay and emit fires more often but not always.

On server I have next code:

io.on('connection', (socket) => {
    let userData = socketsData.get(socket)
    console.log(`${socket.id} connected`);
    // ... other events
    socket.on("personal_chat", async () => {
        console.log(`subscribe to ${userData.chatId} chat`);
        socket.join(`personal_chat_${userData.chatId}`);
    });
    socket.on("disconnect", () => {
        console.log(`${socket.id} disconnected`);
        socketsData.delete(socket);
    });
});

Server logs:

Sep 22 18:01:40 clickhouse-lk node[11900]: F0S-22mkUan2Hw_gAAAF connected
Sep 22 18:01:41 clickhouse-lk node[11900]: F0S-22mkUan2Hw_gAAAF disconnected
Sep 22 18:01:43 clickhouse-lk node[11900]: e5mEbd7zH5SeTtTsAAAG connected
Sep 22 18:01:44 clickhouse-lk node[11900]: e5mEbd7zH5SeTtTsAAAG disconnected
Sep 22 18:01:45 clickhouse-lk node[11900]: dAF09biIm0s7YxXgAAAH connected
Sep 22 18:01:48 clickhouse-lk node[11900]: dAF09biIm0s7YxXgAAAH disconnected

As you can see, I tried to send emit several times, but nothing reaches the server. I tried to do it from the JS client and everything is emitted fine. What could be the problem and how can it be solved?

WGPavell avatar Sep 23 '21 06:09 WGPavell