Socket.IO-Client-Swift icon indicating copy to clipboard operation
Socket.IO-Client-Swift copied to clipboard

Connect to remote server

Open malik-ben opened this issue 10 years ago • 18 comments

Hello, For some reason, i cannot connect socket.io to a remote server. When using localhost it worked fine, but when switching, nothing happens.

Any help?

malik-ben avatar May 18 '15 18:05 malik-ben

how are you trying to connect?

nuclearace avatar May 18 '15 21:05 nuclearace

Did you add the right port number when you are creating instance of the SocketIOClient?

sauvikatinnofied avatar May 19 '15 16:05 sauvikatinnofied

https://github.com/nuclearace/Socket.IO-Client-Swift/issues/53

nuclearace avatar May 21 '15 18:05 nuclearace

Possibly not a duplicate.

nuclearace avatar May 21 '15 23:05 nuclearace

+1 - same for me, tried using it in a fresh sample project in view did load or in viewcontroller method

func connectSocket() {

    let mySocket = SocketIOClient(socketURL: "\(CurrentConfiguration.serverURL)")
    mySocket.connect()

    mySocket.onAny{println("got event: \($0.event) with items \($0.items)")}

    mySocket.on("connect") {data, ack in
        println("socket connected")
    }


}

longbowww avatar May 27 '15 16:05 longbowww

@longbowww That socket object is scoped to that function, once it leaves it will be released. You need to make the socket a property of an object.

nuclearace avatar May 27 '15 16:05 nuclearace

@nuclearace allright, got it now :)

longbowww avatar May 27 '15 19:05 longbowww

@longbowww hey I have the same issue did you declare as var ? let mySocket = SocketIOClient(socketURL: "(CurrentConfiguration.serverURL)") outside the function ?

public func scala_init(host: String, uuid: String, secret: String, token: String) -> Promise<Bool> { return Promise { fulfill, reject in hostUrl=host Alamofire.Manager.sharedInstance.session.configuration.HTTPAdditionalHeaders = ["Authorization": token]; //socket connection // socket = SocketIOClient(socketURL: "test.test.com:9000", options: [String : AnyObject]?()) socket.on("connect") {data, ack in println("socket connected") fulfill(true) } socket.connect() } } @nuclearace How can I do this by passing the host??

cesar-oyarzun-m avatar Sep 09 '15 19:09 cesar-oyarzun-m

@cesar-oyarzun-m i ended up doing the socket connect in a view controller and declare "var socket" in it

var socket = SocketIOClient(socketURL: "\(CurrentConfiguration.serverURL)", opts: [
"reconnects": true,
"reconnectAttempts": 5, 
        "reconnectWait": 5,
        "nsp": "me",
        "forceWebsockets": true, 
        "log":false
        ])

override func viewDidLoad() {
    super.viewDidLoad()

    self.configureSocket()

}

func configureSocket() {

    self.socket.on("connect") {data, ack in

        println("socket connected")

    }

    self.socket.onAny {

        println("Got event: \($0.event), with items: \($0.items)")

    }

    //whatever socket.on events you wanna have here

    self.socket.connect()
}

longbowww avatar Sep 09 '15 21:09 longbowww

@longbowww CurrentConfiguration.serverURL is other swift class?

cesar-oyarzun-m avatar Sep 09 '15 21:09 cesar-oyarzun-m

@cesar-oyarzun-m its just a String from another class https://serverUrl.com:port

longbowww avatar Sep 09 '15 21:09 longbowww

@longbowww mmm I'm trying to make this dynamic not static, the host will be received as parameter in the method. I have this code it doesnt work

public final class ScalaSocket: NSObject {

var socket = SocketIOClient(socketURL: "test.server.com:9000", options: ["log": true])

public func start_socket(){
    self.socket.on("connect") {data, ack in
        println("socket connected")
    }
    self.socket.connect()
}
} 

I'm calling this from other method

public func scala_init(host: String, uuid: String, secret: String, token: String)  -> Promise<Bool> {
    return Promise { fulfill, reject in
    var scalaSocket = ScalaSocket()
    scalaSocket.start_socket()
    }
}

cesar-oyarzun-m avatar Sep 10 '15 13:09 cesar-oyarzun-m

what does the log say? does it do anything?

longbowww avatar Sep 10 '15 20:09 longbowww

Your scoping is wrong. scalaSocket is getting released inside the promise closure.

nuclearace avatar Sep 10 '15 20:09 nuclearace

yeah thanks is working now just problem with the scope :+1:

cesar-oyarzun-m avatar Sep 10 '15 20:09 cesar-oyarzun-m

Hey @nuclearace I'm trying to pass the socket object to other object, but I think the scope is only for one class, I have this

public  class TestSocket: NSObject {
public var socket = SocketIOClient(socketURL: hostUrl, options: ["log": false,"connectParams": ["token":tokenSDK]])

Then I have other class the inherit from the one that has the socket variable and I want to override the request method, but the emit doesn't work

public final class TestSystemCh: TestSocket {

override public func request(messageDic: Dictionary<String, String>) -> Promise<Any> {
    var dic:Dictionary<String,String> = messageDic as Dictionary
    dic.updateValue("system", forKey: "channel")
    let requestPromise = Promise<Any> { fulfill, reject in
        socket.emit(ScalaConfig.SOCKET_MESSAGE,dic)
        var promiseDic = Dictionary<String,Any>()
        promiseDic  = [ "fulfill": fulfill,"reject":reject]
        request.updateValue(promiseDic, forKey: uuid)
    }
    return requestPromise
}

}

cesar-oyarzun-m avatar Sep 16 '15 16:09 cesar-oyarzun-m

Are you sure that the socket is connected when you're trying to emit? Also, you're writing some Objective-C looking Swift code

override public func request(var dic: [String: String]) -> Promise<Any> {
    dic["channel"] = "system"
    let requestPromise = Promise<Any> { fulfill, reject in
        socket.emit(ScalaConfig.SOCKET_MESSAGE,dic)
        var promiseDic = Dictionary<String,Any>()
        promiseDic  = [ "fulfill": fulfill,"reject":reject]
        request.updateValue(promiseDic, forKey: uuid)
    }
    return requestPromise
}

If it is connected, it's probably some scope problem again

nuclearace avatar Sep 16 '15 17:09 nuclearace

@nuclearace I'm doing this on Swift code and socket is not connected if I ask on the child class, I understand must me a scope problem, but I don't know how to solved, Do I have to create child with the socket as var or pass as parameter?

cesar-oyarzun-m avatar Sep 16 '15 17:09 cesar-oyarzun-m