Tuesday, 2 May 2017

Unable to make basic socket.io emit work with Swift

I've looked at a couple demos that overlapped quite a bit but I'm not able to get a basic emit to work. I get a connection message with the server file but nothing on an attempt to emit from the client. Here's what I have in each file:

SocketIOManager.swift

import UIKit

class SocketIOManager: NSObject {

    static let sharedInstance = SocketIOManager()

    override init() {
        super.init()
    }

    var socket: SocketIOClient = SocketIOClient(socketURL: NSURL(string: "myURL")! as URL)

    func establishConnection() {
        socket.connect()
    }


    func closeConnection() {
        socket.disconnect()
    }

    func testSocket() {
        socket.emit("test")
    }

}

AppDelegate.swift

func applicationDidEnterBackground(_ application: UIApplication) {
   SocketIOManager.sharedInstance.closeConnection()
}

func applicationDidBecomeActive(_ application: UIApplication) {
   SocketIOManager.sharedInstance.establishConnection()

}

ViewController

override func viewDidLoad() {
        super.viewDidLoad()      
        SocketIOManager.sharedInstance.testSocket()
    }

index.js server file

io.on('connection', function(clientSocket){
  console.log('a user connected');


  clientSocket.on('disconnect', function(){
    console.log('user disconnected');
    }

  clientSocket.on("test", function(){
    console.log('test')
  });

});

So again, while I'm getting the print statements for connection and disconnection, I'm getting nothing for a regular emit function.



via SuperCodeBrah

No comments:

Post a Comment