c#client和node.js服务器之间的数据传输问题

时间:2015-06-05 19:57:38

标签: javascript c# node.js server transfer

我正在写一个聊天,有一个node.js服务器和一些c#客户端,有池(每10秒一个请求) - 使用Backgroundworker,没什么特别的。 但事实证明有点令人不安,因为我能够一方面将数据从客户端发送到服务器,但仍然无法发送从服务器返回客户端的消息。

到目前为止,这是我的服务器:

 var http = require("http");
var qs = require("querystring");
var fileSystem = require("fs");
var portNr = 81;
http.createServer(onRequest).listen(portNr, onready);
var feld = [];


function onRequest(request, response) {
   var body = "";
   var obj = {
       msg:"df",
       sentby:"df"
   };


   request.on("data", function(data) {
        if(data!=""){
            obj = JSON.parse(data);

        }
   });
   request.on("end", function() {
       if(undefined!=obj) //as there are requests every 10 seconds {
           response.writeHead(200,{'Content-Type': 'text/plain'});
           console.log(obj.msg);
           response.write(obj.msg);
           response.end();
         }
       else //send nothing back if nothing was requested
        response.end();
   });


}
function onready() {

    console.log("Server laeuft");
}

这是我的c#客户端,首先是我的汇集方法:

public void Pooling(object sender, DoWorkEventArgs e)
        {
            while (run)
            {
                WebRequest request = WebRequest.Create("http://localhost:81");
                request.Credentials = CredentialCache.DefaultCredentials;
               WebResponse response = request.GetResponse();
               Stream dataStream = response.GetResponseStream();
               StreamReader reader = new StreamReader(dataStream);


               string responseFromServer = reader.ReadToEnd();

               //Message n = new JavaScriptSerializer().Deserialize<Message>(responseFromServer);
               reader.Close();
               response.Close();

               Dispatcher.Invoke((Action)delegate()
               {
                   if (responseFromServer != "")
                   {
                       this.listbox.Items.Add(responseFromServer);
                       System.Threading.Thread.Sleep(1000);
                   }
                });
              }

        }

现在我的方法将在&#39;发送&#39;单击按钮:

 var request = (HttpWebRequest)WebRequest.Create("http://localhost:81");
           MemoryStream stream1 = new MemoryStream();
           StreamReader sr = new StreamReader(stream1);
           Message = new Message(this.msg.Text, this.sentby.Text);


           System.Runtime.Serialization.Json.DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Message));
           ser.WriteObject(stream1, n);
           stream1.Position = 0;

           string hilf = sr.ReadToEnd();
           var data = Encoding.ASCII.GetBytes(hilf);

           request.Method = "POST";
           request.ContentType = "application/x-www-form-urlencoded";
           request.ContentLength = data.Length;

           using (var stream = request.GetRequestStream())
           {
               stream.Write(data, 0, data.Length);
           }


       }

您可能已经注意到我正在使用Json。 我可以在node.js服务器中显示已发送的消息,但他确实将其添加到c#客户端的列表框中! 请有人吗?在此先感谢:)

0 个答案:

没有答案