SwiftR icon indicating copy to clipboard operation
SwiftR copied to clipboard

how to overcome: Error Domain=WKErrorDomain Code=5

Open mowali opened this issue 8 years ago • 16 comments

I'm running this SignalR server in my PC: https://github.com/adamhartford/SignalRDemo and I am trying to hit it from my swift client: https://github.com/adamhartford/SwiftR

but I get this error: Starting... 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.

my version of signalR is properly set as: public static var signalRVersion: SignalRVersion = .v2_2_0

I am pointing to my pc server name (abc) in the hub connection: hubConnection = SwiftR.connect("http://abc:8080") { [weak self] connection in and my server is running waiting for hits.

mowali avatar May 18 '16 23:05 mowali

That means http://abc:8080 is not reachable by your client. Need to make sure abc resolves properly. Either add it to your hosts file or use the IP address in the url instead.

That said, I do need to make sure SwiftR can process those types of errors properly.

adamhartford avatar May 19 '16 00:05 adamhartford

I changed my ip address, but still the same error. The app I am working on has "networking" and i use my other pc server name and resolves just fine. what else should I try!??

mowali avatar May 19 '16 01:05 mowali

I'm assuming you're using the simulator for now. Can you browse to http://abc:8080/signalr from iOS (device or simulator)?

Also, when the app is running in the simulator, open Safari on the Mac and choose Develop --> Simulator --> SwiftR.framework. Then tap Start in the SwiftR iOS demo app and look for any output in the Safari web developer console. That should give you a hint.

adamhartford avatar May 19 '16 01:05 adamhartford

Develop in the safari!? there is only file, edit, history, bookmark window and help.

mowali avatar May 19 '16 02:05 mowali

got it. development tools like chrome and IE, doing it now...

mowali avatar May 19 '16 02:05 mowali

does this mean anything? [Error] Error: SignalR: Connection must be started before data can be sent. Call .start() before .send() send (jquery.signalr-2.2.0.min:8:11474) invoke (jquery.signalr-2.2.0.min:8:34507) global code (SwiftR.framework:1) [Error] Failed to load resource: Origin null is not allowed by Access-Control-Allow-Origin. (negotiate, line 0) [Error] XMLHttpRequest cannot load http://192.168.1.109:8080/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&_=1463625661924. Origin null is not allowed by Access-Control-Allow-Origin.

mowali avatar May 19 '16 02:05 mowali

That implies that CORS is not enabled on your server. See the README for a short explanation of what to do. If you can't enable CORS for whatever reason, you can set SwiftR.useWKWebView = false in your client code.

adamhartford avatar May 19 '16 03:05 adamhartford

I will right now and let you know what happens. Also, in firewall, I found that my destination computer is using that port of 8080 for private access. I can delete it, should I or just use another port like 5000 instead and not bother!? Could that have something to do with it?

mowali avatar May 19 '16 03:05 mowali

I changed the port from 8080 to 5000 and opened it at the server. Now I am seeing more activities:

  1. the error is changed to read Unable to set user agent for UIWebView. Please register defaults via NSUserDefaults instead. Log: Starting... Error: Optional({ message = "Error during negotiation request."; source = { readyState = 4; responseText = ""-//W3C//DTD HTML 4.01//EN\"\"http://www.w3.org/TR/html4/strict.dtd\"> \n<HTML><HEAD><TITLE>Bad Request</TITLE> \n<META HTTP-EQUIV="Content-Type" Content="text/html; charset=us-ascii"></HEAD> \n<BODY>

    Bad Request - Invalid Hostname

    \n

    HTTP Error 400. The request hostname is invalid.

    \n</BODY></HTML> \n"; status = 400; statusText = "Bad Request"; }; }) 2016-05-18 22:48:52.260 SwiftR iOS Demo[6517:22745163] void SendDelegateMessage(NSInvocation *): delegate (webView:decidePolicyForNavigationAction:request:frame:decisionListener:) failed to return after waiting 10 seconds. main run loop mode: kCFRunLoopDefaultMode Disconnected.

the error comes from this section: connection.error = { error in print("Error: (error)")

            // Here's an example of how to automatically reconnect after a timeout.
            //
            // For example, on the device, if the app is in the background long enough
            // for the SignalR connection to time out, you'll get disconnected/error
            // notifications when the app becomes active again.

            if let source = error?["source"] as? String where source == "TimeoutException" {
                print("Connection timed out. Restarting...")
                connection.start()
            }
        }

mowali avatar May 19 '16 03:05 mowali

The only important thing there is the HTTP Error 400. It indicates that you're trying to connect on port 5000, but your SignalR app isn't prepared to listen on port 5000. You most likely need a code change in your server app for the new port.

I think you should try to work with your SignalR app in a web browser first. Once you can verify it's working with a simple JavaScript client, then you try a SwiftR client. It'll help to rule out all server issues first.

adamhartford avatar May 19 '16 04:05 adamhartford

I've been working mostly with javascript client that locally calls a signalR on an app and was able to put an name and chat with other windows and no problem, just because I am hitting it locally. But I will do it again since I have your SwiftR client working on my Mac. Do you have a working sample for signalr app tutorial? 2nd question: when you said that my signal app is not "prepared" what do you mean by that? is there more than switching the 8080 port with 5000 port!?

mowali avatar May 19 '16 04:05 mowali

I did what you suggested. I had web-based Chat Hub sample (not console app). I hosted the site in my iis and was able to have a page client in the pc and a safari client in the iMac to talk to each other. This is the link they are talking through: http://abc/HubSample/Dafault.html so, we know that the pc and mac can resolve just fine, right!? What's next? open port 8080 on the abc pc server? We want to start mimicking what the swift client will look for, right!?

mowali avatar May 19 '16 18:05 mowali

the default.html had this js code: $(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 = $('

').text(name).html(); var encodedMsg = $('
').text(message).html(); // Add the message to the page. $('#discussion').append('
  • ' + encodedName + ':  ' + encodedMsg + '
  • '); }; // 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(); }); }); });

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

    mowali avatar May 19 '16 20:05 mowali

    From the URL you showed (http://abc/HubSample/Dafault.html), you're using port 80. Why are you trying 8080 or 5000 with SwiftR? If you have some SignalR stuff already working, you should be using the same URL, probably just http://abc. It's really hard for me to tell without looking at your server code. I can just tell you that SwiftR can't connect to the URL you're specifying.

    What do you get when you enter the SwiftR URL in a browser?

    adamhartford avatar May 20 '16 11:05 adamhartford

    I was just trying to model after all the samples always hitting a specific port, but if I can keep it in the 80 port, I'm OK with that. here is a video I created today to show you how I can access my C# in my PC from entering a url in my safari browser in my mac, and how it worked. But I can't do the same connection from my SwiftR

    If the url in the video is hazy: url in Safri: http://filepc/HubSample/Default.html

    https://www.youtube.com/watch?v=Mk9rGOLe1Cg

    mowali avatar May 23 '16 02:05 mowali

    I was trying to load the WebView using the webview_flutter plugin. It's working fine on Android device but at a time of execution of method from JavaScript file on iOS device, I'm facing this issue,

    [VERBOSE-2:ui_dart_state.cc(199)] Unhandled Exception: PlatformException(evaluateJavaScript_failed, Failed evaluating JavaScript

    Platforms: Flutter : 2.2.3 Plugin : webview_flutter 2.0.10, http 0.13.3 iOS : 13.0

    nikhilkWinjit avatar Aug 26 '21 04:08 nikhilkWinjit