Socket.IO-Client-Swift
Socket.IO-Client-Swift copied to clipboard
Connect to remote server
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?
how are you trying to connect?
Did you add the right port number when you are creating instance of the SocketIOClient?
https://github.com/nuclearace/Socket.IO-Client-Swift/issues/53
Possibly not a duplicate.
+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 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 allright, got it now :)
@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 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 CurrentConfiguration.serverURL is other swift class?
@cesar-oyarzun-m its just a String from another class https://serverUrl.com:port
@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()
}
}
what does the log say? does it do anything?
Your scoping is wrong. scalaSocket is getting released inside the promise closure.
yeah thanks is working now just problem with the scope :+1:
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
}
}
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 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?