【问题标题】:Pusher for Windows Phone failing to disconnectWindows Phone 的 Pusher 无法断开连接
【发布时间】:2015-06-29 08:24:01
【问题描述】:

我遇到了从 Nuget 安装的 pusherClient.wp8 包的问题。每次将应用程序发送到后台时,我都会断开与推送器的连接并取消订阅频道。当我恢复应用程序时,当使用推送器重新建立连接时代码挂起。

我尝试重置 pusher 对象,重置频道,创建 pusher 的新实例,但仍然没有任何效果,似乎包有问题,或者更确切地说是 websocket 断开方法无法与 pusher 断开连接,但是,当我关闭应用程序时,一切都会重置。但这在用户从我的应用打开照片选择器的情况下对我没有帮助。

有没有人建议或知道我可以用于 Windows Phone 8 的另一个 pusherclient。我已经为这个问题苦苦挣扎了好几个星期了。 这是我使用的包的github链接:https://github.com/bszypelow/PusherClient.WP8/blob/master/PusherClient.WP8

谢谢

public ConnectionState Connect()
{
    var task = Task.Run(async () => await pusher.ConnectAsync()); ;
    try
    {
        task.Wait();
    }
    catch (Exception ex)
    {
        Debug.WriteLine("Exception " + ex.Message + " at " + ex.Source + "Inner exception " + ex.InnerException + " additional data " + ex.Data);
    }
    return task.Result;
}

【问题讨论】:

  • 挂什么方法?是订阅还是订阅频道?
  • 挂起的方法是ConnectAsync()
  • 嗯,这与我描述的问题相同。
  • 你能把调用这个方法的代码贴出来吗?
  • 我现在将编辑帖子以插入代码

标签: c# silverlight windows-phone-8 deadlock pusher


【解决方案1】:

通过查看源代码我有一个猜测:

所有异步方法从不使用.ConfigureAwait(false),它可能是reason for the dead lock. 特别是如果您使用.Wait() 或.Result 调用it from the UI thread。以事件为例。

我建议你更新代码(它是 MIT 许可证,你可以这样做):

public Task<Channel> Subscribe(string channelName)
{
    if (_connection.State != ConnectionState.Connected)
        throw new PusherException("You must wait for Pusher to connect before you can subscribe to a channel", ErrorCodes.NotConnected);

    if (Channels.ContainsKey(channelName))
    {
        return Task.FromResult(Channels[channelName]);
    }

    // If private or presence channel, check that auth endpoint has been set
    var chanType = ChannelTypes.Public;

    if (channelName.ToLower().StartsWith("private-"))
        chanType = ChannelTypes.Private;
    else if (channelName.ToLower().StartsWith("presence-"))
        chanType = ChannelTypes.Presence;

    return SubscribeToChannel(chanType, channelName); //await is not needed here
}

private async Task<Channel> SubscribeToChannel(ChannelTypes type, string channelName)
{
    switch (type)
    {
        case ChannelTypes.Public:
            Channels.Add(channelName, new Channel(channelName, this));
            break;
        case ChannelTypes.Private:
            AuthEndpointCheck();
            Channels.Add(channelName, new PrivateChannel(channelName, this));
            break;
        case ChannelTypes.Presence:
            AuthEndpointCheck();
            Channels.Add(channelName, new PresenceChannel(channelName, this));
            break;
    }

    if (type == ChannelTypes.Presence || type == ChannelTypes.Private)
    {
        string jsonAuth = await _options.Authorizer.Authorize(channelName, _connection.SocketID)
         .ConfigureAwait(false); //do not capture the context!!

        var template = new { auth = String.Empty, channel_data = String.Empty };
        var message = JsonConvert.DeserializeAnonymousType(jsonAuth, template);

        _connection.Send(JsonConvert.SerializeObject(new { @event = Constants.CHANNEL_SUBSCRIBE, data = new { channel = channelName, auth = message.auth, channel_data = message.channel_data } }));
    }
    else
    {
        // No need for auth details. Just send subscribe event
        _connection.Send(JsonConvert.SerializeObject(new { @event = Constants.CHANNEL_SUBSCRIBE, data = new { channel = channelName } }));
    }

    return Channels[channelName];
}

您可以查看源代码,如果您现在不使用它,您可以安装ReSharper 和this plug-in 的试用版。他们将帮助您找到所有缺少.ConfigureAwait 的行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-04
    • 1970-01-01
    • 2019-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多