当我在Websync publish方法中传递Client.clientId时出现问题,当客户端的OnReceive事件捕获时,clientId有时会变为零。
我是否知道为什么有时会通过通道成功发送clientId值,但通常clientId值会以某种方式变为零,如00000000-0000-0000-0000-000000000000。
以下是我的代码
订阅
var client = new fm.websync.client('http://localhost:12345/websync');
client.connect
({
onSuccess: function (e) {
$('#connectionStat').val('Successfully connected');
},
onFailure: function (e) {
$('#connectionStat').val('Failed to be connected');
},
onStreamFailure: function (e) {
$('#connectionStat').val('Failed to be connected on stream');
}
});
client.subscribe({
channel: '/testService/clientId',
onSuccess: function (e) {
},
onFailure: function (e) {
},
onReceive: function (e) {
alert(e.getData());
}
});
发布
Client client = new Client("http://localhost:12345/websync");
client.Connect(new ConnectArgs
{
OnSuccess = (e) =>
{
Debug.WriteLine("Successfully connected [OnSuccess]");
},
OnFailure = (e) =>
{
Debug.WriteLine("Failed in connected [OnFailure]");
},
OnComplete = (e) =>
{
Debug.WriteLine("Successfully connected [OnComplete]");
},
OnStreamFailure = (e) =>
{
Debug.WriteLine("Failed connected [OnStreamFailure]");
}
});
client.Publish(new PublishArgs("/testService/clientId", Json.Serialize(client.ClientId))
{
OnSuccess = (e) =>
{
Debug.WriteLine("Successfully published client id [OnSuccess] = " + client.ClientId.ToString());
},
OnComplete = (e) =>
{
Debug.WriteLine("Successfully published [OnComplete]");
},
OnFailure = (e) =>
{
Debug.WriteLine("Failed to publish [OnFailure]");
},
});
答案 0 :(得分:0)
等到Publish
,直到Connect
成功完成。客户端的ID由服务器生成,当调用Publish
时,Connect
操作可能尚未完成。
最简单的方法是将Publish
来电转移到OnSuccess
的{{1}}回调中。