如何使用signalr

时间:2015-08-03 17:59:33

标签: signalr signalr-hub signalr.client

在我目前的项目中,我想在子域上构建消息传递系统。假设有buyer.xyz.com和seller.xyz.com,买方和卖方可以互相发送消息,没有用户角色,买方和卖方都来自不同的表。当买方发送消息时,消息被插入消息表中,如果指定的卖方当前在线,则应该更新,反之亦然。我是信号员的新手。如果可能,请提供代码示例。

1 个答案:

答案 0 :(得分:2)

基本上,最好的入门方式是官方文档: http://www.asp.net/signalr/overview/guide-to-the-api/hubs-api-guide-javascript-client#crossdomain

您应该将Microsoft.Owin.Cors库添加到项目中。然后在您的启动类中,修改只有Configuration的{​​{1}}方法。 (请注意,代码直接来自SignalR文档 - 您还可以指定它将接受连接的域/子域。)

  

app.MapSignalR()

然后,假设您对JavaScript API感兴趣,请指定函数的URL:

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 uncommenting 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);
            });

希望这有帮助!祝你好运!