1

Is there a way to emulate in Fiddler or Swift what this js send to signalr:

$(function () {
    // Declare a proxy to reference the hub.
    var chat = $.connection.chatHub;
    // Create a function that the hub can call to broadcast messages.
    chat.client.broadcastMessage = function (name, message) {
        // Html encode display name and message.
        var encodedName = $('<div />').text(name).html();
        var encodedMsg = $('<div />').text(message).html();
        // Add the message to the page.
        $('#discussion').append('<li><strong>' + encodedName
            + '</strong>:&nbsp;&nbsp;' + encodedMsg + '</li>');
    };
    // Get the user name and store it to prepend to messages.
    $('#displayname').val(prompt('Enter your name:', ''));
    // Set initial focus to message input box.
    $('#message').focus();
    // Start the connection.
    $.connection.hub.start().done(function () {
        $('#sendmessage').click(function () {
            // Call the Send method on the hub.
            chat.server.send($('#displayname').val(), $('#message').val());
            // Clear text box and reset focus for next comment.
            $('#message').val('').focus();
        });
    });
});

that page is residing in an HTML accessed via my iis server: http://abc/HubSample/ (abc is my server name) This is the C# code it calls:

public class ChatHub : Hub
{
    public void Send(string name, string message)
    {
        // Call the broadcastMessage method to update clients.
        Clients.All.broadcastMessage(name, message);
    }
}

I can call that html page just fine from other servers too. The goal is to include different clients (not just js ones) in calling that method.

I've tried in Swift calling it like this:

// Default is false
SwiftR.useWKWebView = true

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

// Hubs...
hubConnection = SwiftR.connect("http://abc/HubSample") { [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("broadcastMessage") { args in
        let message = args![0] as! String
        let detail = args![1] as! String
        print("Message: \(message)\nDetail: \(detail)\n")
    }

But I keep getting this error in XCode and Safari dev tools:

SwiftR unable to process message h0z81qhh: 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}

XMLHttpRequest cannot load http://abc/HubSample/signalr/negotiate?clientProtocol=1.5&foo=bar&connectionData=%5B%7B%22name%22%3A%22simplehub%22%7D%2C%7B%22name%22%3A%22complexhub%22%7D%5D&_=1463691894451. Origin null is not allowed by Access-Control-Allow-Origin.

Calling it from Swift is ideal but would be happy seeing it accessible from any other client (other than javascript)

has anyone tried to do this!?

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
user1019042
  • 2,428
  • 9
  • 43
  • 85
  • Well if you were using pure websockets - you won't have such problems. With signalR however you have to write your own client for the platforms where there is no default client yet (by reading their spec\documentation and\or looking to the implementation of their .NET client). – Evk May 24 '16 at 07:29
  • hmm... OK, I am very open to that. I will search about it. Do you have links/keywords I should include in my search!? – user1019042 May 24 '16 at 22:24

2 Answers2

1

I could replicate what you wanted to do in Fiddler. Plug it in your fiddler. It did trigger the breaking point

http://localhost/HubSample/signalr/send?transport=serverSentEvents&connectionToken=Ez8n0rVK%2Bw%2F90yiLke%2Fv0jNiVkUi0fbJfNB3oLfhPb1QZqS5zrwZ6jY1kLGU0AISolwmjROEpxkgz0Otj72%2Bkjh8OgsE9KXZUybGPENX9rfQgK2y8IrFsveKpl7C75Wm&connectionData=%5B%7B%22name%22%3A%22chathub%22%7D%5D data={"H":"chathub","M":"Send","A":["joe","2"],"I":1}

user3126427
  • 855
  • 2
  • 14
  • 29
0

That error means you have one of the following problems:

  1. Your server http://abc is not reachable from your device.
  2. You've configured SwiftR to expect SignalR version 2.2.0, but you're really running some other version, like 2.0.0.
  3. You're using SwiftR with WKWebView, but you have not enabled CORS on your server.

Update to SwiftR version 0.11.0 and you'll see a better error message.

Adam
  • 741
  • 6
  • 4