简体   繁体   中英

IOS Native connecting signalR Hub

I am trying to connect to my server hub with a ios client. It works if I use the tutorial client provided in javascript. However, what is the path I must use in order to connect to the hub.

Here is the client IOS native code

NSString *url = [NSString stringWithFormat:@"http://10.0.0.120:7363/ChatHub"];
SRHubConnection *hubConnection = [SRHubConnection connectionWithURL:url];

 Start the connection
[hubConnection start];

Here is my server code. I have a break point in the OnConnected() that never gets called.

public class ChatHub : Hub
{
 public override Task OnConnected() {
 return base.OnConnected();  
}
 }

The file on the server is called ChatHub and it is not in any folder. I is directly under the project file. Here is my output when I try to run it

Thread {name = (null), num = 1}:-[SRConnection negotiate:] [Line 145] [CONNECTION] will negotiate

I think it has something to do with my urlpath.

I get this error

  Error Domain=AFNetworkingErrorDomain Code=-1011 "Request failed: internal server error (500)"    UserInfo=0x10b545230 {NSLocalizedDescription=Request failed: internal server error (500), NSErrorFailingURLKey=http://10.0.0.120:7363/signalr/signalr/negotiate?clientProtocol=1.3.0.0&connectionData=%5B%7B%22Name%22%3A%22broadcastmessage%22%7D%5D, AFNetworkingOperationFailingURLResponseErrorKey=<NSHTTPURLResponse: 0x10c907f50> { URL: http://10.0.0.120:7363/signalr/signalr/negotiate?clientProtocol=1.3.0.0&connectionData=%5B%7B%22Name%22%3A%22broadcastmessage%22%7D%5D } { status code: 500, headers {
"Cache-Control" = private;
"Content-Type" = "text/html; charset=utf-8";
Date = "Wed, 28 May 2014 23:09:45 GMT";
Server = "Microsoft-IIS/8.0";
"Transfer-Encoding" = Identity;
"X-AspNet-Version" = "4.0.30319";
"X-Powered-By" = "ASP.NET";
} }, NSUnderlyingError=0x10b54b550 "Request failed: unacceptable content-type: text/html"}

I was able to get this to work by changing the client pod spec to use a branch of DyKnow/SignalR-ObjC ' feature-2.0.0.beta1 ' and then added some code to set the content type to application/json for all requests that start with "/signalr" in there path.

class Startup
{

    public void Configuration(IAppBuilder app)
    {       
        app.Use(async (env, next) => // owin middleware
            {
                //set headers
                env.Response.OnSendingHeaders( state =>
                {
                    PathString pathstring = new PathString("/signalr");
                    if (env.Request.Path.HasValue && env.Request.Path.StartsWithSegments(pathstring))
                    {
                        var resp = (OwinResponse)state;
                        resp.ContentType = "application/json";
                    }

                }, env.Response);

                await next();

            });

        app.RunSignalR();

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM