javascript - SingalR Error during negotiation request - Stack Overflow

I've got a project with SignalR and AspNet. I'm trying to connect my client (its a cors) and

I've got a project with SignalR and AspNet. I'm trying to connect my client (its a cors) and the first request returns 200 code but I'm getting this error in my client side:

Error during negotiation request.

My SignalR Server side classes:

public class Startup1
{
    public void Configuration(IAppBuilder app)
    {
        // Branch the pipeline here for requests that start with "/signalr"
        app.Map("/signalr", map =>
        {
            // Setup the CORS middleware to run before SignalR.
            // By default this will allow all origins. You can 
            // configure the set of origins and/or http verbs by
            // providing a cors options with a different policy.
            map.UseCors(CorsOptions.AllowAll);
            var hubConfiguration = new HubConfiguration
            {
                // You can enable JSONP by unmenting line below.
                // JSONP requests are insecure but some older browsers (and some
                // versions of IE) require JSONP to work cross domain
                EnableJSONP = true
            };
            // Run the SignalR pipeline. We're not using MapSignalR
            // since this branch already runs under the "/signalr"
            // path.
            map.RunSignalR(hubConfiguration);
        });
    }
}

My client-side js code:

<script src="@Arbor.CVC.Common.Common.BuildServerFilePath("inc/js/jquery.signalR-2.2.3.min.js")">
</script>
<script type="text/javascript">
        $(document).ready(function () {
               var username = "";
               var id = "";
               var connection = $.hubConnection();
               var contosoChatHubProxy = connection.createHubProxy('Chat');

               connection.url = 'http://localhost:64585/signalr';
               connection.start().done(function () { 
                         console.error('Now connected, connection ID=' + connection.id); }).fail(function (e) { 
                         console.error('Could not connect ' + e); });
 </script>

The Request gives this answer:

Url /signalr
ConnectionToken BwSsXO+oHqBNh7kqklTWTawIR7/Do3Rc4N+48KrCNzZLB37PlP0V+DnCYgW9EguJsYcjUAf6lhqz3LNd1hqJNxGJHHWbssn4YZEZQBNqeOPC8Ex7ndJfEvEfGslEvCDI
ConnectionId    352c6a53-64b9-4b45-85ce-ae7d20b33ba9
KeepAliveTimeout    20
DisconnectTimeout   30
ConnectionTimeout   110
TryWebSockets   true
ProtocolVersion 1.4
TransportConnectTimeout 5
LongPollDelay   0

But still I get the error of negotiation.

I've got a project with SignalR and AspNet. I'm trying to connect my client (its a cors) and the first request returns 200 code but I'm getting this error in my client side:

Error during negotiation request.

My SignalR Server side classes:

public class Startup1
{
    public void Configuration(IAppBuilder app)
    {
        // Branch the pipeline here for requests that start with "/signalr"
        app.Map("/signalr", map =>
        {
            // Setup the CORS middleware to run before SignalR.
            // By default this will allow all origins. You can 
            // configure the set of origins and/or http verbs by
            // providing a cors options with a different policy.
            map.UseCors(CorsOptions.AllowAll);
            var hubConfiguration = new HubConfiguration
            {
                // You can enable JSONP by unmenting line below.
                // JSONP requests are insecure but some older browsers (and some
                // versions of IE) require JSONP to work cross domain
                EnableJSONP = true
            };
            // Run the SignalR pipeline. We're not using MapSignalR
            // since this branch already runs under the "/signalr"
            // path.
            map.RunSignalR(hubConfiguration);
        });
    }
}

My client-side js code:

<script src="@Arbor.CVC.Common.Common.BuildServerFilePath("inc/js/jquery.signalR-2.2.3.min.js")">
</script>
<script type="text/javascript">
        $(document).ready(function () {
               var username = "";
               var id = "";
               var connection = $.hubConnection();
               var contosoChatHubProxy = connection.createHubProxy('Chat');

               connection.url = 'http://localhost:64585/signalr';
               connection.start().done(function () { 
                         console.error('Now connected, connection ID=' + connection.id); }).fail(function (e) { 
                         console.error('Could not connect ' + e); });
 </script>

The Request gives this answer:

Url /signalr
ConnectionToken BwSsXO+oHqBNh7kqklTWTawIR7/Do3Rc4N+48KrCNzZLB37PlP0V+DnCYgW9EguJsYcjUAf6lhqz3LNd1hqJNxGJHHWbssn4YZEZQBNqeOPC8Ex7ndJfEvEfGslEvCDI
ConnectionId    352c6a53-64b9-4b45-85ce-ae7d20b33ba9
KeepAliveTimeout    20
DisconnectTimeout   30
ConnectionTimeout   110
TryWebSockets   true
ProtocolVersion 1.4
TransportConnectTimeout 5
LongPollDelay   0

But still I get the error of negotiation.

Share Improve this question edited May 15, 2018 at 9:52 patricia asked May 15, 2018 at 8:58 patriciapatricia 1,1033 gold badges19 silver badges44 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

I removed the cors from the Startup:

public class Startup1
{
    public void Configuration(IAppBuilder app)
    {
        // Branch the pipeline here for requests that start with "/signalr"
        app.Map("/signalr", map =>
        {
            // Setup the CORS middleware to run before SignalR.
            // By default this will allow all origins. You can 
            // configure the set of origins and/or http verbs by
            // providing a cors options with a different policy.
            //map.UseCors(CorsOptions.AllowAll);
            var hubConfiguration = new HubConfiguration
            {
                // You can enable JSONP by unmenting line below.
                // JSONP requests are insecure but some older browsers (and some
                // versions of IE) require JSONP to work cross domain
                EnableJSONP = true,
                EnableJavaScriptProxies = true,
                EnableDetailedErrors = true
            };
            // Run the SignalR pipeline. We're not using MapSignalR
            // since this branch already runs under the "/signalr"
            // path.
            map.RunSignalR(hubConfiguration);
        });
        app.MapSignalR();
    }
}

And added the tag [HubName("Chat")]to my Chat.cs class.

Also I needed to define the origin, I couldn't use the *.

<system.webServer>
    <httpProtocol>
      <customHeaders>
          <add name="Access-Control-Allow-Origin" value="http://localhost:27947" />
          <add name="Access-Control-Allow-Methods" value="*" />
          <add name="Access-Control-Allow-Credentials" value="true" />
       </customHeaders>
    </httpProtocol>
</system.webServer>

In the JS:

var connection = $.hubConnection();
var contosoChatHubProxy = connection.createHubProxy('Chat');

connection.url = 'http://localhost:64585/signalr';
connection.start({ transport: ['webSockets', 'longPolling'] }).done(function () {console.log('Now connected, connection ID=' + connection.id);}).fail(function (e) { console.error('Could not connect ' + e); });

If you need to allow more than one origin use this piece of code for web.config (IIS):

<system.webServer>
    <httpProtocol>
      <customHeaders>    
        <add name="Access-Control-Allow-Methods" value="*" />
        <add name="Access-Control-Allow-Credentials" value="true" />
      </customHeaders>
    </httpProtocol>
    <rewrite>            
        <outboundRules>
            <clear />                
            <rule name="AddCrossDomainHeader">
                <match serverVariable="RESPONSE_Access_Control_Allow_Origin" pattern=".*" />
                <conditions logicalGrouping="MatchAll" trackAllCaptures="true">
                    <add input="{HTTP_ORIGIN}" pattern="(http(s)?://((.+\.)?localhost:27947|(.+\.)?localhost:26928))" />
                </conditions>
                <action type="Rewrite" value="{C:0}" />
            </rule>           
        </outboundRules>
    </rewrite>
  </system.webServer>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745140401a4613402.html

相关推荐

  • javascript - SingalR Error during negotiation request - Stack Overflow

    I've got a project with SignalR and AspNet. I'm trying to connect my client (its a cors) and

    1小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信