WebSync发布ClientId变为零

时间:2018-01-25 07:53:19

标签: javascript c# websync

当我在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]");
       },
});

1 个答案:

答案 0 :(得分:0)

等到Publish,直到Connect成功完成。客户端的ID由服务器生成,当调用Publish时,Connect操作可能尚未完成。

最简单的方法是将Publish来电转移到OnSuccess的{​​{1}}回调中。