socketio / socket.io-client-swift

Other
5.22k stars 842 forks source link

Connect to server success but cannot connect to channel or listen event to get data. #1387

Open sonhd92 opened 3 years ago

sonhd92 commented 3 years ago

This is the first time I'm working with SocketIO and have socket info like this:

Link socket: https://dev-cpl-backend-echo-auth-v2.staging-bitcastle.work;
Channel: App.Orders_USDT_BTC
Event: App\\Events\\OrderTransactionCreated

I wrote a wrapper class for handling socket connections, which connected to a socket successfully but cannot listen to an event.:

import SocketIO

class CPLSocketManager: NSObject {
static let sharedSocket = CPLSocketManager()

var socket: SocketIOClient!
private let manager = SocketManager(socketURL: URL(string: "https://dev-cpl-backend-echo-auth-v2.staging-bitcastle.work/")!, config: [.log(true), .compress])

private override init() {
    super.init()
    socket = manager.defaultSocket
}

func startSocket() {
    socket.on(clientEvent: .connect) { data, ack in
        print(data)
        print("-----SOCKET CONNECTED-----")
    }

    socket.on(clientEvent: .error) { data, ack in
        print(data)
        print("-----SOCKET ERROR-----")
    }

    socket.on(clientEvent: .disconnect) { data, ack in
        print(data)
        print("-----SOCKET DISONNECT-----")
    }

    socket.on(clientEvent: .reconnect) { data, ack in
        print(data)
        print("-----SOCKET RECONNECT-----")
    }

    socket.connect()
}

func stopSocket() {
    socket.disconnect()
}

func connectToChannel(channelName: String, eventName: String) {
    let dict = ["channel": channelName]
    socket.emit(eventName, with: [dict])
    socket.on(channelName) { data, ack in
        print(">>channel>>>\(data)>>>>>>>>>")
    }
}

func listen(eventName: String) {
    socket.on(eventName) { data, ack in
        print("==event==\(data)>>>>>>>>>>>")
    }
}
}

In my viewcontroller I set the channel name and event name:

CPLSocketManager.sharedSocket.connectToChannel(channelName: "App.Orders_USDT_BTC", eventName: "App\\Events\\OrderTransactionCreated")
    CPLSocketManager.sharedSocket.listen(eventName: "App\\Events\\OrderTransactionCreated")

But when build and run the project then goes to my screen. The socket log shows:

2021-11-03 11:24:14.454206+0700 BitcastleApp[26848:813310] LOG SocketIOClient{/}: Emitting: 2["App\\Events\\OrderTransactionCreated",{"channel":"App.Orders_USDT_BTC"}], Ack: false
2021-11-03 11:24:14.454292+0700 BitcastleApp[26848:813410] LOG SocketEngine: Writing ws: 2["App\\Events\\OrderTransactionCreated",{"channel":"App.Orders_USDT_BTC"}] has data: false
2021-11-03 11:24:17.933986+0700 BitcastleApp[26848:813310] LOG SocketIOClient{/}: Adding handler for event: App.Orders_USDT_BTC
2021-11-03 11:24:17.934048+0700 BitcastleApp[26848:813410] LOG SocketEngineWebSocket: Sending ws: 2["App\\Events\\OrderTransactionCreated",{"channel":"App.Orders_USDT_BTC"}] as type: 4
2021-11-03 11:24:24.582901+0700 BitcastleApp[26848:813310] LOG SocketIOClient{/}: Adding handler for event: App\Events\OrderTransactionCreated
2021-11-03 11:24:24.582922+0700 BitcastleApp[26848:813410] LOG SocketEngine: Writing ws:  has data: false
2021-11-03 11:24:24.583134+0700 BitcastleApp[26848:813410] LOG SocketEngineWebSocket: Sending ws:  as type: 2
2021-11-03 11:24:24.584207+0700 BitcastleApp[26848:813403] [boringssl] boringssl_metrics_log_metric_block_invoke(144) Failed to log metrics
2021-11-03 11:24:24.584404+0700 BitcastleApp[26848:813399] Connection 64: missing error, so heuristics synthesized error(1:53)
2021-11-03 11:24:24.584442+0700 BitcastleApp[26848:813399] Connection 64: encountered error(1:53)
2021-11-03 11:24:24.615079+0700 BitcastleApp[26848:813310] [TableView] Changing the background color of UITableViewHeaderFooterView is not supported. Use the background view configuration instead.
2021-11-03 11:24:24.622056+0700 BitcastleApp[26848:813310] LOG SocketIOClient{/}: Handling event: ping with data: []
2021-11-03 11:24:24.736974+0700 BitcastleApp[26848:813399] LOG SocketEngine: Got message: 3
2021-11-03 11:24:24.737061+0700 BitcastleApp[26848:813310] LOG SocketIOClient{/}: Handling event: pong with data: []

So, what's wrong with my code?

GustavoAlvarez commented 1 year ago

@sonhd92 Did you solve it?

WZoptal commented 1 year ago

which ios version that you were using and also which is the server side version with server side language

Nikunjkumar-R commented 6 months ago

Was anyone able to solve this?