gpt4 book ai didi

javascript - 信号员 - 与不同的客户

转载 作者:行者123 更新时间:2023-11-28 06:18:23 25 4
gpt4 key购买 nike

我正在尝试设置 signalR 系统。

我使用两个浏览器和相同的集线器运行示例代码。发送和接收消息。

现在,当我创建一个不同的页面并尝试将消息发送到集线器时,它似乎有点工作,这意味着它不会崩溃,但没有任何内容传输到其他客户端。

我以为我正在从所有客户端访问同一个消息中心,但也许我错过了一些东西。

是否可以将不同的网站连接到同一个消息中心?

开始编辑

根据要求...这是我在第二个客户端上使用的代码...

  var connection = $.hubConnection('http://xxxxxxxxx.azurewebsites.net/');
var contosoChatHubProxy = connection.createHubProxy('MessagePump');
// contosoChatHubProxy.on('Send', function (name, message) {console.log(name + ' ' + message);});


$.connection.hub.start()
.done(function () {
console.log('Now connected, connection ID=' + $.connection.hub.id); // returns an ID
// $.connection.hub.send('testing', 'this is a test from the client');
// contosoChatHubProxy.send("testing");
// contosoChatHubProxy.invoke('testing', 'this is a test for the client 1');
// contosoChatHubProxy.invoke('say', 'this is a test for the client 2');
// contosoChatHubProxy.invoke('Send', 'This is a test for client 3');
// $.connection.hub.send('testing', 'this is a test from the client 4');
contosoChatHubProxy.invoke('messagePump', 'user', 'this is a test message for 5');
})
.fail(function(){ console.log('Could not Connect!'); });

这就是我在 Firebug 中看到的

enter image description here

根据我对代码的了解,代理似乎正在本地加载,甚至看不到远程系统中心...

我的控制台应用程序仅连接到远程系统集线器才能发送和接收消息。

顺便说一句 - 我已经尝试过大写和小写(MessagePump,messagePump)但它并没有改变结果。

最佳答案

 var connection = $.hubConnection('http://xxxxxxxxx.azurewebsites.net/');

您正在尝试连接不同的网站。这个http://xxxxxxxxx.azurewebsites.net/应该允许跨域请求。否则你无法连接。如果您可以管理http://xxxxxxxxx.azurewebsites.net/,则应该配置信号器,如下所示:

   public class Startup
{
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 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);
});
}
}

关于javascript - 信号员 - 与不同的客户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35751296/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com