问题描述
我写了这个简单的code这 - 当连接时,应产生
I wrote this simple code which --when connected , should yield
- 第一!
- 欢迎
- 3消息(定时器)
这是code:
int i = 0;
protected override Task OnConnected(IRequest request, string connectionId)
{
//first message
Connection.Send(connectionId, "FIRST !"); //first message
//last messages
Timer _timer = new Timer(RunMe, new {con = connectionId, req = request}, 2000, 1000);
//second message
return Connection.Send(connectionId, "Welcome!"); //second message
}
void RunMe(dynamic state)
{
if (i < 3)
{
Connection.Send((string) state.con, "Loop " + i);
i++;
}
}
和这里的JS code(的document.ready
块中):
and here the JS code ( inside document.ready
block) :
...
connection.received(function (data)
{
$('#messages').append('<li><b>data received = </b>' + data +
"<b>ConnectionId =</b> " + connection.id +
'<b>MessageId = </b>' + connection.messageId + '</li>');
});
我得到所有响应。
但是,如果我preSS F5(刷新) - 这是典型的回答:
But if I press f5(refresh) -- these are the typical responses :
再次刷新,
看来,第一条消息的从不得到了 MESSAGEID
:
It seems that the first message never gets a messageID
:
和后来的消息有时获取MESSAGEID和有时不行。
And later messages sometimes get messageID and sometimes not.
我心想,也许这是一个初始化速度的问题,所以我尝试这样的:
I thought to myself , maybe it's an initialization speed problems , so I tried this :
再次,回应是:
- 这是怎么回事?为什么第一个消息没有得到MESSAGEID?我该如何解决呢?
推荐答案
在SignalR 1.1,connection.messageId设置 所有connection.received处理程序已经呼吁一批消息后。
In SignalR 1.1, connection.messageId is set after all of the connection.received handlers have been called for a batch of messages.
消息ID真的只是为了被SignalR内部使用,但在SignalR被称为connection.received处理程序之前2.0 connection.messageId将被设置。
Message IDs are really only meant to be used by SignalR internally, but in SignalR 2.0 connection.messageId will be set before the connection.received handlers are called.
这篇关于在signalR首先任意的消息没有MESSAGEID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!