【问题标题】:Windows phone 8 signalR connection methodWindows phone 8 signalR 连接方法
【发布时间】:2014-01-31 01:18:47
【问题描述】:

我是在 Windows 8 平台上编程的新手,所以我被困住了。在我的项目中,我使用 SignalR(我正确下载并安装了软件包)并尝试使用以下代码简单地实现连接:http://code.msdn.microsoft.com/wpapps/Windows-Phone-8-Chat-1fa5eccf

        public Play()
    {
        InitializeComponent();

        _connection = new HubConnection(websiteUrl);
        _myHub = _connection.CreateHubProxy("GameManager");
        InitializeConnection();
    }

    public async Task InitializeConnection()
    {
        try
        {
            var obj = new
            {
                header = 0,
                data = App.login,

            };
            var result = JsonConvert.SerializeObject(obj, Formatting.Indented);

            await _connection.Start();
            await _myHub.Invoke("SendConnection", result);
            IsConnected = true;

            _myHub.On<string>("recieved", data =>
            {
                if (OnRecieved != null)
                {
                    OnRecieved(this, new ChatEventArgs { MessageSent = data });
                    System.Diagnostics.Debug.WriteLine("DATA : " + data);
                }
            });

            if (OnConnected != null)
            {
                OnConnected(this, new EventArgs());
            }
        }
        catch
        {

        }
    } 

websiteUrl 是我要访问的网站的 URL。

但是,我没有成功接收到来自服务器的响应(我知道如果连接成功,它会返回一些东西)。

我需要做更多或不同的事情吗?感谢您的帮助。

【问题讨论】:

    标签: c# mobile windows-phone-8 signalr


    【解决方案1】:

    请在开始连接之前分配事件。所以事件将被正确映射。我已经用异常变量捕获了异常,以查看您收到的错误。您还必须检查在您的信号托管应用程序中定义的身份验证方法。如果它基于 cookie,那么您必须从 windows phone 传递 cookie 详细信息。否则禁用服务器代码上的身份验证。

    公共播放() { 初始化组件();

        _connection = new HubConnection(websiteUrl);
        _myHub = _connection.CreateHubProxy("GameManager");
        InitializeConnection();
    }
    
    public async Task InitializeConnection()
    {
        try
        {
            var obj = new
            {
                header = 0,
                data = App.login,
    
            };
            var result = JsonConvert.SerializeObject(obj, Formatting.Indented);
    
           _myHub.On<string>("recieved", data =>
            {
                if (OnRecieved != null)
                {
                    OnRecieved(this, new ChatEventArgs { MessageSent = data });
                    System.Diagnostics.Debug.WriteLine("DATA : " + data);
                }
            });
    
    
            await _connection.Start();
            await _myHub.Invoke("SendConnection", result);
            IsConnected = true;
    
    
            if (OnConnected != null)
            {
                OnConnected(this, new EventArgs());
            }
        }
        catch(Exception ex)
        {
    
        }
    } 
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-27
    • 2013-01-04
    • 1970-01-01
    • 2013-01-15
    • 1970-01-01
    • 2012-10-21
    • 1970-01-01
    相关资源
    最近更新 更多