2012-08-23 27 views
0

問題: 服務器應向客戶端發送SerialPort命令(例如「S」)並接收端口。發送另一個命令(例如「r01」)。向客戶端發送消息並接收對接收到的答案的答案+行爲

示例:服務器發送「S」並從客戶端收到「N」,以便服務器發送另一個「S」。如果收到!=「N」,則發送「r01」等。

我可以發送消息給客戶,但我無法收到答案並將其存儲/使用。

我試過至今:

)保存接收到的信息轉化爲serv.connectedClients [ID] .receive

服務器: 工人階級:

public void doSomething(Service serv, string ID) 
    { 
     serv.SendMessageToClient(client_name, "S"); //send first message      
     data = serv.connectedClients[ID].receive.ToString(); //the .receive is empty but should have the clients answer stored 
     if(data != ""){ 
      serv.SendMessageToClient(client_name, "r01"); //send second message 
      serv.connectedClients[ID].Information = serv.connectedClients[ID].receive.ToString(); 
      } 
    } 

在服務.cs:

public void getPortMsg(string msg) 
    { 
     connectedClients[OperationContext.Current.Channel.SessionId].receive = msg; 
    } 

在客戶端:

void callback_OnMessageReceivedEvent(string message) 
    { 
     string portmsg = ""; 
     portmsg = rfid.send(message); //portmsg gets the port.readline info 
     client.getPortMsg(portmsg); //send portmsg to server 

    } 

而且rfid.send:

public string send(string senden) 
    { 

     string data = ""; 
     port.WriteLine(senden); 
     data = port.ReadLine(); 

      return data; 
     } 

順序:服務器將消息發送給客戶端。客戶端收到該消息並調用其中包含答案的服務方法。服務器應該使用這個答案,但是在他得到答案之前發送下一條消息。

希望有人知道答案。

回答

0

我有一個答案。我不認爲這是解決問題的最佳方法,但它的工作原理如下:

服務器向客戶端發送消息+方法的名稱。

客戶端與消息一起工作並向服務器寫入答案+方法的名稱。

服務器現在知道如何處理答案並調用正確的方法。

代碼例如:

服務器:

public void methodA(){ 
    sendToClient("i want an answer",methodA); 
} 

客戶:

public void callback_messageFromServer(string msg,string location){ 
    string answer = workWithMsg(msg); 
    client.sendAnswer(answer,location); 
} 

服務器:

public void sendAnswer(string msg, string location) { 
     if(location == "methodA") 
      methodB(msg); 
} 

要呼叫我使用int計數和開關相同的方法(計數)。 希望它可以幫助某人^^

致以問候

相關問題