none
SignalR Client 接收訊息(PersistentConnection) RRS feed

  • 問題

  • 我的專案為Visual Studio 2010,.net framework 4.0,SignalR version為1.2.2(Server,Client版本都一樣)

    hubConnection = new HubConnection("http://localhost:6684/realtime/echo/signalr");

    hubConnection.Received += ReceivedMessage;

    hubConnection.Start().Wait();

    我的問題是ReceivedMessage一直沒收到Server來的訊息,但是Server有收到Client的訊息,不曉得是我使用(HubConnection與PersistentConnection)錯誤的關係嗎?

    我的專案完整程式碼,放在以下的目錄下,請大家幫我看一下,我已經卡了幾天了,謝謝!

    https://onedrive.live.com/redir?resid=FBEB6373D9321A7F!268774&authkey=!AJFy5y12XY0vOGI&ithint=file%2czip

    Server為ASP.NET empty project建立的專案,Global.asax如下:

    	public class Global : System.Web.HttpApplication
    	{
    
    		protected void Application_Start(object sender, EventArgs e)
    		{
    			//RouteTable.Routes.MapHubs();
    			RouteTable.Routes.MapConnection<EchoConnection>("EchoConnection", "/realtime/echo");
    		}

    EchoConnection.cs 如下,我是用PersistentConnection,不是用Hub:

    	public class EchoConnection : PersistentConnection
    	{
    		private static int _connections = 0;
    
    		protected override Task OnConnected(IRequest request, string connectionId)
    		{			
    			Interlocked.Increment(ref _connections);
    			string sendMsg = "Setting," + connectionId;
    			return Connection.Send(connectionId, sendMsg);
    		}
    
    		protected override Task OnDisconnected(IRequest request, string connectionId)
    		{
    			Interlocked.Decrement(ref _connections);
    			return null;
    		}
    
    		protected override Task OnReceived(IRequest request, string connectionId, string data)
    		{
    			string[] strArray = data.Split(',');
    
    			string szCommand = strArray[0];
    
    			if (szCommand == "abc")
    			{
    				try
    				{
    					//return Connection.Broadcast("def");
    					return Connection.Send(connectionId, "def");
    				}
    				catch (Exception ex)
    				{
    					Debug.WriteLine("Exception  = " + ex.ToString());
    					return null;
    				}
    			}
    
    			return null;
    
    		}
    
    	}
    Client端是Windows Form(VS2010, .net 4.0)
    	public partial class SignalClient : Form
    	{
    		public HubConnection hubConnection;
    		public IHubProxy hubProxy;
    
    		private delegate void AddTxt(string msg);
    
    		public SignalClient()
    		{
    			InitializeComponent();
    
    			hubConnection = new HubConnection("http://localhost:6684/realtime/echo/signalr");
    
    			//hubProxy = hubConnection.CreateHubProxy("EchoConnection");
    			//hubProxy.On<string>("addMessage", (message) => this.Invoke(new AddTxt(ReceivedMessage), message));
    			//hubProxy.On<string>("Received", (message) => this.Invoke(new AddTxt(ReceivedMessage), message));
    			//hubConnection.Received += data =>
    			//    {
    			//        ReceivedMessage(data);
    			//    };
    			hubConnection.Received += ReceivedMessage;
    			hubConnection.Start().Wait();
    			
    		}
    
    		private void ReceivedMessage(string str)
    		{
    			Debug.WriteLine("str = " + str);
    		}
    
    		private void button1_Click(object sender, EventArgs e)
    		{
    			string ii = hubConnection.ConnectionId;
    			hubConnection.Send("abc").Wait();
    			//hubProxy.Invoke<string>("chatter", "Hi!! Server");
    		}
    
    	}

    2014年11月20日 下午 01:45

解答

所有回覆

  • 您的電腦需要透過Proxy上網嗎? 如果是的話, 可以看一下這一篇:HubConnection::Start().Wait() times out if behind web proxy

    2014年11月21日 上午 03:09
  • 的確,目前是有透過Proxy,不過昨天我在家裡測試Client端也接不到Server的訊號。

    我也試著將HubConnection的Start的Wait移除,改由StateChanged從Connecting到Connected設定Received函式,但是Client端還是收不到Server端的訊號。

    			hubConnection.StateChanged += data =>
    			{
    				StateChange sc = new StateChange(Microsoft.AspNet.SignalR.Client.ConnectionState.Connecting, Microsoft.AspNet.SignalR.Client.ConnectionState.Connected);
    
    				if (data.NewState == sc.NewState && data.OldState == sc.OldState)
    				{
    					hubConnection.Received += data1 =>
    						{
    							ReceivedMessage(data1);
    						};
    				}
    			};
    
    			hubConnection.Start();
    

    2014年11月21日 上午 06:16
  • 我在Server端的時候,是加了"/realtime/echo"的url

    RouteTable.Routes.MapConnection<EchoConnection>("EchoConnection", "/realtime/echo");

    在Client端的時候,網址也加了"/realtime/echo"

    hubConnection = new HubConnection("http://localhost:6684/realtime/echo");

    不知道是否是這段"/realtime/echo" url的影響?

    2014年11月21日 上午 06:20
  • 終於解決了,原來Persistent Connection的Client端要用Connection,不是HubConnnection

    public Connection hubConnection;

    hubConnection = new Connection("http://localhost:6684/realtime/echo");

    • 已標示為解答 akira32 2014年11月21日 下午 12:23
    2014年11月21日 下午 12:23