ios - 从 Mac 中的 ios 客户端调用 PC 中的 signalR 服务器

标签 ios asp.net swift signalr

我得到了这个服务器代码形式 here和来自同一作者的这个客户here

因此 .Net signalR 服务器在 PC 上运行。 signalR 在控制台应用程序中自托管。客户端是在我的 iMac 中用 Swift 编写的 ios 客户端。我正在尝试使 Mac 中的代码在 PC 中查看代码。 我发现的所有示例都是关于从同一台服务器调用信号器的,这没有帮助。我的服务器网址是:http://locahost:8080我电脑上的 signalR 是这样的:

class MainClass
{
    public static void Main(string[] args)
    {
        // 127.0.0.1 myserver.com in /etc/hosts
        // Or change myserver.com to localhost
        // Using myserver.com instead of localhost to capture requests in Charles Proxy
        string url = "http://localhost:8080";
        using (WebApp.Start (url)) {
            Console.WriteLine ("Server running on {0}", url);
            Console.ReadLine ();
        }
    }
}

这是我尝试使用的方法('abc' 是我的服务器名称):

// Simple example...
[CustomAuthorize]
public class SimpleHub : Hub
{
    public void SendSimple(string message, string detail)
    {
        var clients = Clients.All;
        Clients.All.notifySimple (message, detail);
    }
}

现在的目标实际上是从 iMac 中的 ios 客户端实现它:

override func viewDidLoad() {
    super.viewDidLoad()

    // Make sure myserver.com is mapped to 127.0.0.1 in /etc/hosts
    // Or change myserver.com to localhost or IP below

    // Default is false
    SwiftR.useWKWebView = true

    // Default is .Auto
    SwiftR.transport = .ServerSentEvents

    // Hubs...
    hubConnection = SwiftR.connect("http://abc:8080") { [weak self] connection in
        connection.queryString = ["foo": "bar"]
        connection.headers = ["X-MyHeader1": "Value1", "X-MyHeader2": "Value2"]

        // This only works with WKWebView on iOS >= 9
        // Otherwise, use NSUserDefaults.standardUserDefaults().registerDefaults(["UserAgent": "SwiftR iOS Demo App"])
        connection.customUserAgent = "SwiftR iOS Demo App"

        self?.simpleHub = connection.createHubProxy("simpleHub")
        self?.complexHub = connection.createHubProxy("complexHub")

        self?.simpleHub.on("notifySimple") { args in
            let message = args![0] as! String
            let detail = args![1] as! String
            print("Message: \(message)\nDetail: \(detail)\n")
        }

但是当我尝试启动连接时,我在 Swift 中遇到了这个错误:

SwiftR unable to process message 4eg2u0fw: Error Domain=WKErrorDomain Code=5 "JavaScript execution returned a result of an unsupported type" UserInfo={NSLocalizedDescription=JavaScript execution returned a result of an unsupported type} Disconnected.

关于如何解决这个问题的任何想法!?

最佳答案

我已经改变了方向,开始使用这个信号代码进行探索:Tutorial: Getting Started with SignalR 2 .我在 azure 中托管它,并能够从 Swift 中的 WKWebView 代码调用它。检查我的线程。他们有大量的代码。

关于ios - 从 Mac 中的 ios 客户端调用 PC 中的 signalR 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37311853/

相关文章:

ios - 是否有任何库可以在 iPhone 中使用 AVCaptureSession 获取 vorbis 格式的音频数据?

ios - 格式化没有千位分隔符的双数?

ios - 在 Swift 中查找类的兄弟 View

ios - 仅允许从 UITextField 中选择和复制不起作用

ios - 如何正确设置分页 UICollectionView 大小变化的动画

iphone - 在边界内倾斜移动图像

javascript - 从 Asp.net 文本框中使用 Eval 函数作为参数调用 javascript 函数

asp.net - 通过局域网连接到 Visual Studio 调试 IIS Express 服务器

c# - 仅在垂直滚动时卡住 gridview 标题

string - 如何在 Swift 中将 NSHTTPURLResponse 转换为字符串