簡體   English   中英

連接到信號器集線器

[英]connect to signalr hub

我在控制台應用程序中創建了一個簡單的SignalR集線器:

class Program
{
    static void Main(string[] args) 
    {    
        using (WebApp.Start<Startup>("http://localhost:1968"))
        {
            Console.WriteLine("Server running!");        
            Console.ReadLine();    
        } 
    } 
}

public static class UserHandler
{
    public static HashSet<string> ConnectedIds = new HashSet<string>();
}

[HubName("echo")]
public class EchoHub : Hub 
{ 
    public void Say(string message) 
    { 
        Trace.WriteLine("hub: "+message);
        Clients.All.AddMessage(message);
    }

    public override Task OnConnected()
    {
        UserHandler.ConnectedIds.Add(Context.ConnectionId);
        return base.OnConnected();
    }

    public override Task OnDisconnected()
    {
        UserHandler.ConnectedIds.Remove(Context.ConnectionId);
        return base.OnDisconnected();
    }
}

class Startup 
{ 
    public void Configuration(IAppBuilder app) 
    { 
        app.MapSignalR();
    } 
} 

當我嘗試從Silverlight應用程序連接它時,它成功:

static Microsoft.AspNet.SignalR.Client.HubConnection signalR { get; set; }
public static Microsoft.AspNet.SignalR.Client.IHubProxy signalRhub { get; set; }

public static void StartSignalR()
{
    var url = "http://localhost:1968";
    signalR = new Microsoft.AspNet.SignalR.Client.HubConnection(url);
    signalR.Received += signalR_Received;
    signalRhub = signalR.CreateHubProxy("echo");
    signalR.Start().Wait();
    signalRhub.Invoke("Say", "hub invoked");
}

我的下一步是從jquery連接SignalR集線器:

<script src="../Scripts/jquery-1.6.4.js"></script>
<script src="../Scripts/jquery.signalR-2.1.0.js"></script>
<script>
    $(function ()
    {
        var connection = $.hubConnection("http://localhost:1968");
        connection.start()
            .done(function () {
                console.log('connected');
                connection.send("success?");
            })
            .fail(function (a) {
                console.log('not connected'+a);
            });
    });
</script>

當我嘗試運行此腳本時,它會給出錯誤:

"XMLHttpRequest cannot load localhost:1968/signalr/negotiate?clientProtocol=1.4&_=1404978593482. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin <code>http://localhost:55282</code> is therefore not allowed access."

為什么我可以從Silverlight頁面連接到集線器(托管在localhost:3926中)
當我運行jquery腳本(托管在localhost:55282)時失敗了嗎?

如何在jQuery和SignalR集線器之間建立有效連接?

更改

$(function ()
    {
    var connection = $.hubConnection("http://localhost:1968");
    connection.start()
        .done(function () {
            console.log('connected');
            connection.send("success?");
        })
        .fail(function (a) {
            console.log('not connected'+a);
        });
});

$(function ()
{
var connection = $.hubConnection("http://localhost:1968");
var hub = connection.createHubProxy("echo");
hub.on("AddMessage", Method);
connection.start({ jsonp: true })
            .done(function () {
            console.log('connected');
            hub.say("success?");
        })
        .fail(function (a) {
            console.log('not connected'+a);
        });
});

function Method(messageFromHub)
{
alert(messageFromHub);
}

class Startup 
{ 
    public void Configuration(IAppBuilder app) 
    { 
        app.MapSignalR();
    } 
} 

class Startup 
{ 
    public void Configuration(IAppBuilder app) 
    { 
        app.MapSignalR(new HubConfiguration() { EnableJSONP = true });} 
} 

應該這樣做。

app.MapSignalR(new HubConfiguration() { EnableJSONP = true });} 

connection.start({ jsonp: true })

將允許跨站點請求

使用createHubProxy()在SignalR中使用RPC on Server:

感謝Vishal Ravlani的回答

但對我來說

hub.say("success?");

不起作用! (對你起作用嗎?)

我要寫:

hub.invoke('say','success?');

SignalR已在客戶端自動檢測到CrossOrigin。

在我使用的服務器上:

app.Map("/signalr", map =>
            {
                map.UseCors(CorsOptions.AllowAll);

                map.RunSignalR();
            });

其中描述了: http//www.asp.net/signalr/overview/signalr-20/hubs-api/hubs-api-guide-javascript-client#crossdomain

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM