0

I am trying to connect with my live server for web socket connection. But I am getting below error in iOs.

websocket is disconnected: Optional(Error Domain=NSPOSIXErrorDomain Code=61 "Connection refused" UserInfo={_kCFStreamErrorCodeKey=61, _kCFStreamErrorDomainKey=1})

I have tried with other server but Its working with it. just not with the specified server in my code. both server having https.

Here is my iOS source code link: https://www.dropbox.com/s/v348vxxjf74dsds/websocket.zip?dl=0

import UIKit
import Starscream

class ViewController: UIViewController, WebSocketDelegate {
    @IBOutlet var button: UIButton!
    @IBOutlet var textfiled: UITextField!
    var socket = WebSocket(url: URL(string: "https://103.240.79.191:8080")!)

    override func viewDidLoad() {
        super.viewDidLoad()
        socket.delegate = self
        debugPrint("Called")
        socket.connect()
    }

    func websocketDidConnect(socket: WebSocketClient) {
        print("websocket is connected")
        socket.write(string: "connected")
    }

    func websocketDidReceiveMessage(socket: WebSocketClient, text: String) {
        print("got some text: \(text)")
    }

    func websocketDidDisconnect(socket: WebSocketClient, error: Error?) {
        print("websocket is disconnected: \(error)")
    }

    func websocketDidReceiveData(socket: WebSocketClient, data: Data) {
        debugPrint("Data")
        debugPrint(data)
    }

    @objc func update() {
        debugPrint("Called")
        socket.connect()
    }
    @IBAction func btnclick() {
        socket.write(string: textfiled.text ?? "")
    }
}

Please find the below server side code:

const WebSocket = require('ws');

const wss = new WebSocket.Server({ port: 8080 }, () => {
    console.log("Signaling server is now listening on port 8080")
});

// Broadcast to all.
wss.broadcast = (ws, data) => {
    wss.clients.forEach((client) => {
        if (client !== ws && client.readyState === WebSocket.OPEN) {
            client.send(data);
        }
    });
};

wss.on('connection', (ws) => {
    console.log(`Client connected. Total connected clients: ${wss.clients.size}`)

    ws.onmessage = (message) => {
        console.log(message.data + "\n");
        wss.broadcast(ws, message.data);
    }

    ws.onclose = () => {
        console.log(`Client disconnected. Total connected clients: ${wss.clients.size}`)
    }
});
Simon McLoughlin
  • 8,293
  • 5
  • 32
  • 56
Jignesh Kanani
  • 223
  • 1
  • 4
  • 18
  • Post your code in the question using a code block, don't post a link to a zip. Could need to see your server code too – Simon McLoughlin Jun 06 '19 at 09:42
  • @SimonMcLoughlin Ok let me show you – Jignesh Kanani Jun 06 '19 at 09:51
  • @SimonMcLoughlin Please can you have a look? – Jignesh Kanani Jun 06 '19 at 09:59
  • According to the docs: https://www.npmjs.com/package/ws you are suppose to be connecting to `ws://.....` or `wss://.....` not `http://....` or `https://......` try that – Simon McLoughlin Jun 06 '19 at 10:42
  • @SimonMcLoughlin I tried with wss://..... or ws://..... both are not connecting. I have one more doud is that my server is running on TLS 1.2 so is it reason? What normally will used in iOS like Default TLS 1.0 – Jignesh Kanani Jun 06 '19 at 10:50
  • No TLS 1.2 is the supported version in iOS. They've deprecated the older ones. Have you tried any other clients to make sure the server is setup correctly, you have the correct ip all that crap? Install wscat and run it in command line: https://www.npmjs.com/package/wscat – Simon McLoughlin Jun 06 '19 at 10:54
  • @SimonMcLoughlin I tried with install wscat getting different result with different command Result: https://www.dropbox.com/s/dzzaoa41f90ibio/Untitled.png?dl=0 – Jignesh Kanani Jun 06 '19 at 11:07
  • Thats a link to a "getting started" with dropbox pdf. Please don't post links, you can put the output in a code block or upload images here – Simon McLoughlin Jun 06 '19 at 11:10
  • @SimonMcLoughlin I have updated that link later. I am not able to upload image so given the link. https://www.dropbox.com/s/dzzaoa41f90ibio/Untitled.png?dl=0 – Jignesh Kanani Jun 06 '19 at 11:24
  • You don't need to use the dropbox link at all, simply get the image and upload that in the "upload image" section. Right i've had a look anyway and its a server setup issue then since your command line can't hit it either. Also the IP address in that picture is different to the one in the iOS code above. First ensure that you have the correct IP, use a simpler example without SSL on the ws README documentation and include the `wss.on("error", ....` event incase something is causing an issue with your setup – Simon McLoughlin Jun 06 '19 at 12:52
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/194594/discussion-between-jignesh-kanani-and-simon-mcloughlin). – Jignesh Kanani Jun 07 '19 at 11:36

1 Answers1

-1

It was working with one server but not with another server, And after checking difference of both the server, I have found working server fire wall was off. So i just disable the firewall and its connected.

Jignesh Kanani
  • 223
  • 1
  • 4
  • 18