[英]SignalR client not firing server code
i am testing this signal with a very basic string. 我正在用非常基本的字符串测试此信号。 But the client side is not firing the server code and there is no error. 但是客户端没有触发服务器代码,并且没有错误。 i added the [HubName("MyHub1")]
and the [HubMethodName("GetValueString")]
in because if not the javascript will complaint client undefine and methodname not found. 我在其中添加了[HubName("MyHub1")]
和[HubMethodName("GetValueString")]
,因为如果没有,则javascript会抱怨客户端未定义并且找不到方法名。
after i added this 2 meta in. there is no error but server code was not fire. 在我添加此2 meta后。没有错误,但服务器代码未触发。 Anyone help please. 任何人请帮助。
Client Script 客户端脚本
(function () {
// Defining a connection to the server hub.
debugger
var myHub = $.connection.MyHub1;
// Setting logging to true so that we can see whats happening in the browser console log. [OPTIONAL]
$.connection.hub.logging = true;
// Start the hub
$.connection.hub.start();
// This is the client method which is being called inside the MyHub constructor method every 3 seconds
myHub.client.GetValueString = function (serverTime) {
// Set the received serverTime in the span to show in browser
$("#newTime").html(serverTime);
};
}());
Server script 服务器脚本
[HubName("MyHub1")]
public class MyHub1 : Hub
{
public void Hello()
{
Clients.All.hello();
}
[HubMethodName("GetValueString")]
public void Getstring() {
var taskTimer = Task.Factory.StartNew(async () =>
{
while (true)
{
string timeNow = DateTime.Now.ToString();
//Sending the server time to all the connected clients on the client method ()
Clients.All.GetValueString("test");
//Delaying by 3 seconds.
await Task.Delay(3000);
}
}, TaskCreationOptions.LongRunning
);
}
}
Update 1 so i change my javascript to this and the output was "undefine" but no error also 更新1,所以我将我的JavaScript更改为此,并且输出为“未定义”,但也没有错误
var haha = myHub.client.GetValueString; var haha = myHub.client.GetValueString; // Set the received serverTime in the span to show in browser //设置跨度中接收到的serverTime以在浏览器中显示
Try to give this ago. 尝试给这个。 Also, use this for reference 此外,使用该参考
I've added a console.log
try and see if you see it when you run this code. 我添加了一个console.log
尝试看看在运行此代码时是否看到它。
(function () {
var myHub = $.connection.MyHub1;
myHub.client.GetValueString = function (serverTime) {
$("#newTime").html(serverTime);
};
$.connection.hub.logging = true;
$.connection.hub.start().done(function() {
console.log("hub is ready"); // tell me if you see the message in your console log
myHub.server.getstring() // note i wrote getstring with a small g, has to be.
});
}());
[HubName("MyHub1")]
public class MyHub1 : Hub
{
public void Hello()
{
Clients.All.hello();
}
public void Getstring() {
var taskTimer = Task.Factory.StartNew(async () =>
{
while (true)
{
string timeNow = DateTime.Now.ToString();
//Sending the server time to all the connected clients on the client method ()
Clients.All.GetValueString("test");
//Delaying by 3 seconds.
await Task.Delay(3000);
}
}, TaskCreationOptions.LongRunning
);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.