socketio / socket.io-client-swift

Other
5.21k stars 841 forks source link

socket connection issue in swift 4 #1190

Closed mithun-grmtech closed 5 years ago

mithun-grmtech commented 5 years ago

My server is using socket.io of nodJS. I can not connect server through websocket. The code and log are as follows:

My project version: 4.0 My pod version : 1.5.3

# Uncomment the next line to define a global platform for your project
# platform :ios, '9.0'

target 'my personal app' do
  # Comment the next line if you're not using Swift and don't want to use dynamic frameworks
  use_frameworks!

  pod 'Socket.IO-Client-Swift', '~> 13.0.0'
  pod 'Starscream', '<= 3.0.2'

end

Step 1: I created a new file MySocket.swift

import Foundation
import SocketIO

class MySocket: NSObject {

    let manager = SocketManager(socketURL: NSURL(string: "https://www.abc.com/")! as URL, config: [
        .log(true),
        .compress,
        .reconnects(true),
        .reconnectWait(1)
    ])

    func connected() {
        print("socket try to connecting.....")

        let socket = manager.socket(forNamespace: "/swift")

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

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

        socket.on(“itsMyPersonalChanel”) {data, ack in
            guard let cur = data[0] as? Double else { return }

            print("socket listen for itsMyPersonalChanel : ", cur)

        }

        socket.connect()
    }
}

Step 2: This is controller file HomeViewController.swift

import UIKit

class HomeViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        // Call my socket
        let mySocket = MySocket();
        mySocket.connected();
    }
}

Logs:

2019-04-23 15:46:48.554479+0530 Clinic Check In[7155:375299] LOG SocketIOClient{/swift}: Adding handler for event: connect 2019-04-23 15:46:48.554785+0530 Clinic Check In[7155:375299] LOG SocketIOClient{/swift}: Adding handler for event: error 2019-04-23 15:46:48.554968+0530 Clinic Check In[7155:375299] LOG SocketIOClient{/swift}: Adding handler for event: itsMyPersonalChanel 2019-04-23 15:46:48.555223+0530 Clinic Check In[7155:375299] LOG SocketIOClient{/swift}: Handling event: statusChange with data: [connecting] 2019-04-23 15:46:48.555391+0530 Clinic Check In[7155:375299] LOG SocketManager: Tried connecting socket when engine isn't open. Connecting 2019-04-23 15:46:48.555537+0530 Clinic Check In[7155:375299] LOG SocketManager: Adding engine 2019-04-23 15:46:48.556153+0530 Clinic Check In[7155:375299] LOG SocketManager: Manager is being released 2019-04-23 15:46:48.556213+0530 Clinic Check In[7155:375702] LOG SocketEngine: Starting engine. Server: https://www.abc.com/ 2019-04-23 15:46:48.556339+0530 Clinic Check In[7155:375299] LOG SocketIOClient{/swift}: Client is being released 2019-04-23 15:46:48.556341+0530 Clinic Check In[7155:375702] LOG SocketEngine: Handshaking 2019-04-23 15:46:48.558049+0530 Clinic Check In[7155:375702] LOG SocketEnginePolling: Doing polling GET https://www.abc.com/socket.io/?transport=polling&b64=1 2019-04-23 15:46:49.868672+0530 Clinic Check In[7155:375701] LOG SocketEnginePolling: Got polling response 2019-04-23 15:46:49.868898+0530 Clinic Check In[7155:375701] LOG SocketEnginePolling: Got poll message: 97:0{"sid":"3CH1041hymjxjSRmAAvm","upgrades":["websocket"],"pingInterval":25000,"pingTimeout":60000} 2019-04-23 15:46:49.869308+0530 Clinic Check In[7155:375701] LOG SocketEngine: Got message: 0{"sid":"3CH1041hymjxjSRmAAvm","upgrades":["websocket"],"pingInterval":25000,"pingTimeout":60000} 2019-04-23 15:46:49.872672+0530 Clinic Check In[7155:375701] LOG SocketEngine: Writing poll: has data: false 2019-04-23 15:46:49.872949+0530 Clinic Check In[7155:375701] LOG SocketEnginePolling: Sending poll: as type: 2 2019-04-23 15:46:49.873509+0530 Clinic Check In[7155:375701] LOG SocketEnginePolling: Created POST string: 1:2 2019-04-23 15:46:49.873941+0530 Clinic Check In[7155:375701] LOG SocketEnginePolling: POSTing 2019-04-23 15:46:49.874290+0530 Clinic Check In[7155:375701] LOG SocketEngine: Engine is being released

nuclearace commented 5 years ago

@mithun-grmtech The issue lies in

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        // Call my socket
        let mySocket = MySocket();
        mySocket.connected();
    }

Here you're declaring mySocket as a local variable. The problem with this is mySocket will get released by ARC as soon as the method returns. Which you can see in the logs:

2019-04-23 15:46:48.556153+0530 Clinic Check In[7155:375299] LOG SocketManager: Manager is being released
...
2019-04-23 15:46:48.556339+0530 Clinic Check In[7155:375299] LOG SocketIOClient{/swift}: Client is being released
...
2019-04-23 15:46:49.874290+0530 Clinic Check In[7155:375701] LOG SocketEngine: Engine is being released

You need to make sure the thing that holds the SocketManager is held strongly, and not released by ARC for as long as you want to maintain an active connection.

mithun-grmtech commented 5 years ago

@nuclearace thank you for response

Now it's working fine.