How to connect to ASP.NET Core SignalR socket from iOS app using Starscream?

894 views Asked by At

I am trying to build a simple real-time application, but when ever I connect, it disconnects after a few seconds.

I am also assuming that there must be a way to invoke "Send" method that I implemented on the server. I know that there exists SignalR client for iOS "SignalRObjc" but it causes issues to me.

Questions are: How do I invoke a specific method on the server? Why does it disconnects automatically? Did I make some mistakes in server or client implementations?

// Server // ASP.NET Core SignalR

   public class ChatHub : Hub
   {
    public override Task OnConnectedAsync()
    {
        Debug.WriteLine("Connected cleint with ID :" + Context.ConnectionId);
        return base.OnConnectedAsync();
    }
    public void Send(string message)
    {
        Clients.All.InvokeAsync("New", message);
        Debug.WriteLine("Message invoked");
    }
    public override Task OnDisconnectedAsync(Exception exception)
    {   
        return base.OnDisconnectedAsync(exception);
    }
}

// iOS application

   var socket = WebSocket(url: URL(string: "ws://localhost:50290/api/chat")!)

   func websocketDidConnect(socket: WebSocketClient) {
    print("Socket connected")
   }
   func websocketDidDisconnect(socket: WebSocketClient, error: Error?)        {
    print("Socket disconnected")
}
    func websocketDidReceiveMessage(socket: WebSocketClient, text: String) {
    print(text)
}
    func websocketDidReceiveData(socket: WebSocketClient, data: Data) {
    print(data)
}

}

I also set the socket.delegate = self in the viewDidLoad method and I send a simple message socket.write(string: "hello")

There is a completion block in the socket.write method which never gets called

Also, receive message method is never called, even-though I tried sending something from the server.

0

There are 0 answers