Eric S
Eric S

Reputation: 1031

iOS Websocket Connection to LG Smart TV with Starscream

I'm using Swift to connect an iOS app to a LG Smart TV (both on the same wifi network) via websockets and each time the connection gets a success message and then gets disconnected in less than a second. Below is the code I am using to establish the connection. I masked the TV IP address but have confirmed I am using the correct one.

    public init(){
        self.request = URLRequest(url: URL(string: "ws://192.xxx.x.xx:3000")!)
        self.socket = WebSocket(request: request)
        self.socket.delegate = self
    }
    
    public func connect(){
        print("in connect")
        self.socket.connect()
        print("end of connect")
    }
    
    func didReceive(event: WebSocketEvent, client: WebSocket) {
        switch event {
            case .connected(_):
                print("WebSocket is connected")
            case .disconnected(let reason, let code):
                print("Disconnected: code=\(code), reason=\(reason)")
            case .text(let message):
                print("Received: \(message)")
            case .binary(_):
                print("binary")
                break
            case .pong(_):
                print("pong")
                break
            case .ping(_):
                print("ping")
                break
            case .error(let error):
                print(error ?? "")
            case .viabilityChanged(_):
                print("viability changed")
                break
            case .reconnectSuggested(_):
                print("reconnect suggested")
                break
            case .cancelled:
                print("WebSocket is cancelled")
        }
    }

The connect() method gets called when a button is pressed on a separate view and I can tell it is working based on the print messages. This is the output I get every time

in connect 
end of connect
viability changed
WebSocket is connected
Disconnected: code=1008, reason=invalid origin
WebSocket is cancelled

I've tried searching for this 1008 error code but it seems very generic. I'm new to websockets/network programming and would appreciate any help with this

Upvotes: 2

Views: 935

Answers (0)

Related Questions