Karzel
Karzel

Reputation: 1498

Socket.io with iOS not connecting on the mobile client

I'm trying to create simple socket.io communication between server and iOS client. My server code is pretty simple:

var io = require('socket.io').listen(1337);

io.sockets.on('connection', function (socket) {
    console.log("Someone just connected!");

    // Echo back messages from the client
    socket.on('image', function (message) {
        console.log("Got image of length: " + message.length);
        socket.emit('image', message);
    });

    socket.on('sound', function (message) {
        console.log("Got sound of length: " + message.length);
        socket.emit('sound', message);
    });

    socket.on('pdf', function (message) {
        console.log("Got pdf of length: " + message.length);
        socket.emit('pdf', message);
    });
});

And following the example from : https://github.com/socketio/socket.io-client-swift

I've implemented my client as:

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        setupSocketIO()
    }

    func setupSocketIO(){
        let manager = SocketManager(socketURL: URL(string: "http://127.0.0.1:1337/")!, config: [.log(true), .compress])
        let socket = manager.defaultSocket

        socket.on(clientEvent: .connect) {data, ack in
            print("socket connected")
        }

        socket.on("image") {data, ack in
            guard let cur = data[0] as? String else { return }
            print("got image back: ", cur)
        }

        socket.connect()

        socket.emit("image", "sample")
    }
}

After running the code, the behaviour is really strange, because on the server side I've got in my logs message Someone just connected!, but there are no signs of connection on the client side (that means, neither socket connected, nor sample message in logs seen).

Logs in the XCode:

SocketIOClient{/}: Adding handler for event: connect
SocketIOClient{/}: Adding handler for event: image
SocketIOClient{/}: Handling event: error with data: ["Tried emitting when not connected"]
SocketIOClient{/}: Handling event: statusChange with data: [connecting, 2]
SocketIOClient{/}: Joining namespace /
SocketManager: Tried connecting socket when engine isn't open. Connecting
SocketManager: Adding engine
SocketManager: Manager is being released
SocketEngine: Starting engine. Server: http://127.0.0.1:1337
SocketEngine: Handshaking
SocketIOClient{/}: Client is being relea2
SocketEnginePolling: Doing polling GET http://127.0.0.1:1337/socket.io/?transport=polling&b64=1
SocketEnginePolling: Got polling response
SocketEnginePolling: Got poll message: 96:0{"sid":"4k2wFGd250H88zmYAAAE","upgrades":["websocket"],"pingInterval":25000,"pingTimeout":5000}2:40
SocketEngine: Got message: 0{"sid":"4k2wFGd250H88zmYAAAE","upgrades":["websocket"],"pingInterval":25000,"pingTimeout":5000}
SocketEngine: Got message: 40
SocketEngine: Writing poll:  has data: false
SocketEnginePolling: Sending poll:  as type: 2
SocketEnginePolling: Created POST string: 1:2
SocketEnginePolling: POSTing
SocketEngine: Engine is being released

Upvotes: 4

Views: 6436

Answers (1)

Thao Ngo
Thao Ngo

Reputation: 3771

The current version of the socket io client library for IOS (socketio/socket.io-client-swift:15.x) does not yet support socket io server version 3.x.

Rationale: There was a problem with the default namespace declaration under the new protocol. If you enable debug mode, you will see that Socket server still receives message from client but events such as on connection, emit are not triggered.

Solution: Downgrade your NodeJS socket io server version to version 2.x

Eg: npm install [email protected]

Upvotes: 3

Related Questions