为啥红蜘蛛不再在真正的 Apple Watch (WatchOS 6) 上运行?

Posted

技术标签:

【中文标题】为啥红蜘蛛不再在真正的 Apple Watch (WatchOS 6) 上运行?【英文标题】:Why is Starscream not working anymore on real Apple Watch (WatchOS 6)?为什么红蜘蛛不再在真正的 Apple Watch (WatchOS 6) 上运行? 【发布时间】:2020-02-06 00:20:47 【问题描述】:

最近我将 Apple Watch 升级到 WatchOS 6.0.1,iPhone 升级到 ios 13.1.2,Xcode 升级到 11.1。 MacOS 仍然是 10.14.6。

我创建了一个独立的 Apple Watch 项目,我在其中使用 Starscream 测试 Watch 和 WebSocket 服务器之间的通信:https://github.com/daltoniam/Starscream

在模拟器上完美运行,但在真正的 Apple Watch 上,当我尝试连接到服务器时,我收到这些错误

2019-10-08 18:57:53.064887+0200 BackgroundWebSocketOnlyWatch WatchKit Extension[251:31011] [] nw_connection_get_connected_socket [C1] Client called nw_connection_get_connected_socket on unconnected nw_connection

2019-10-08 18:57:53.068928+0200 BackgroundWebSocketOnlyWatch WatchKit Extension[251:31011] TCP Conn 0x16d8d5f0 Failed : error 0:50 [50]

websocket is disconnected: Optional("The operation couldn’t be completed. Network is down")

我将向您展示我的应用程序的代码:

InterfaceController.swift

import WatchKit
import Foundation
import Starscream

class InterfaceController: WKInterfaceController, WebSocketDelegate 

let socket = WebSocket(url: URL(string: "ws://echo.websocket.org/")!)

@IBOutlet var label: WKInterfaceLabel!

/**************************************************************************************************/

override func awake(withContext context: Any?) 
    super.awake(withContext: context)

    socket.delegate = self


override func willActivate() 
    // This method is called when watch view controller is about to be visible to user
    super.willActivate()


override func didDeactivate() 
    // This method is called when watch view controller is no longer visible
    super.didDeactivate()


/************************************************************************************************/

@IBAction func connectButtonPressed() 

    socket.connect()


@IBAction func sendButtonPressed() 

    socket.write(string: "Hi!")


@IBAction func disconnectButtonPressed() 

    socket.disconnect()


/******************************************************************************************/

func websocketDidConnect(socket: WebSocketClient) 
    print("websocket is connected")
    label.setText("Connected")

func websocketDidDisconnect(socket: WebSocketClient, error: Error?) 
    print("websocket is disconnected: \(error?.localizedDescription)")
    label.setText("Disconnected")

func websocketDidReceiveMessage(socket: WebSocketClient, text: String) 
    print("got some text: \(text)")
    label.setText("Received: \(text)")
    createVibration()


func websocketDidReceiveData(socket: WebSocketClient, data: Data) 
    print("got some data: \(data.count)")

/******************************************************************************************/

// Creates vibration
func createVibration() 

    WKInterfaceDevice.current().play(.notification)
    print("Vibration created")

我尝试在 iOS 13.1.2 中使用 Starscream,它在模拟器和真 iPhone 上都能完美运行

这是 WatchOS 6 的错误还是红蜘蛛需要升级?

提前致谢! :)


编辑: 2019 年 11 月 5 日

我已将 Apple Watch 更新至 WatchOS 6.1,将 iPhone 更新至 iOS 13.2,但 WebSocket 仍然无法在手表上运行...

虽然有一些新错误:

2019-11-05 12:33:29.317757+0100 BackgroundWebSocketBothDevices WatchKit Extension[269:37201] dnssd_clientstub ConnectToServer: connect() failed path:/var/run/mDNSResponder Socket:4 Err:-1 Errno:1 Operation not permitted

2019-11-05 12:33:29.317919+0100 BackgroundWebSocketBothDevices WatchKit Extension[269:37201] [] nw_resolver_create_dns_service_locked [C1] DNSServiceCreateDelegateConnection failed: ServiceNotRunning(-65563)

2019-11-05 12:33:29.319083+0100 BackgroundWebSocketBothDevices WatchKit Extension[269:37255] [] nw_connection_get_connected_socket [C1] Client called nw_connection_get_connected_socket on unconnected nw_connection

2019-11-05 12:33:29.319150+0100 BackgroundWebSocketBothDevices WatchKit Extension[269:37255] TCP Conn 0x155a7360 Failed : error 0:-65563 [-65563]

websocket is disconnected: The operation couldn’t be completed. (kCFErrorDomainCFNetwork error -72000.)

【问题讨论】:

【参考方案1】:

不幸的是,我们的 WatchOS 缺少 kCFStream 属性来完成这项工作。

https://github.com/daltoniam/Starscream/blob/e6b65c6d9077ea48b4a7bdda8994a1d3c6969c8d/Sources/Starscream/WebSocket.swift#L171

【讨论】:

以上是关于为啥红蜘蛛不再在真正的 Apple Watch (WatchOS 6) 上运行?的主要内容,如果未能解决你的问题,请参考以下文章

在真正的 Apple Watch 和无限微调器上运行应用程序

为啥通过 Apple Watch 向 iPhone 发送消息时 print() 不起作用?

为啥我的 Apple Watch 复杂功能不显示任何内容?

为啥我的 iPhone 无法从 Apple Watch 获取心率数据,但手表扩展程序却可以?

为啥我的 iPhone 无法从 Apple Watch 获取心率数据,但手表扩展程序却可以?

为啥我的 Apple Watch OS 应用只有在 iOS 应用处于活动状态时才能收到来自我的 iOS 应用的消息?