将消息发送到Socket,从C#客户端到node.js + socket.io服务器

我想通过套接字与c#windows store应用程序客户端发送消息到node.js和socket.io服务器

我的客户端代码是这样的(C#)

private async void SendDatatoSocket(string sendTextData) { if (!connected) { StatusText = "Must be connected to send!"; return; } Int32 wordlength = 0; // Gets the UTF-8 string length. try { OutputView = ""; StatusText = "Trying to send data ..."; txtblock_showstatus.Text += System.Environment.NewLine; txtblock_showstatus.Text += StatusText; Debug.WriteLine(StatusText); // add a newline to the text to send string sendData = sendTextData + Environment.NewLine; DataWriter writer = new DataWriter(clientSocket.OutputStream); wordlength = sendData.Length; // Gets the UTF-8 string length. // Call StoreAsync method to store the data to a backing stream await writer.StoreAsync(); StatusText = "Data was sent" + Environment.NewLine; txtblock_showstatus.Text += System.Environment.NewLine; txtblock_showstatus.Text += StatusText; Debug.WriteLine(StatusText); // detach the stream and close it writer.DetachStream(); writer.Dispose(); } catch (Exception exception) { // If this is an unknown status, // it means that the error is fatal and retry will likely fail. if (SocketError.GetStatus(exception.HResult) == SocketErrorStatus.Unknown) { throw; } StatusText = "Send data or receive failed with error: " + exception.Message; txtblock_showstatus.Text += System.Environment.NewLine; txtblock_showstatus.Text += StatusText; Debug.WriteLine(StatusText); // Could retry the connection, but for this simple example // just close the socket. closing = true; clientSocket.Dispose(); clientSocket = null; connected = false; } // Now try to receive data from server try { OutputView = ""; StatusText = "Trying to receive data ..."; Debug.WriteLine(StatusText); txtblock_showstatus.Text += System.Environment.NewLine; txtblock_showstatus.Text += StatusText; DataReader reader = new DataReader(clientSocket.InputStream); string receivedData; reader.InputStreamOptions = InputStreamOptions.Partial; var count = await reader.LoadAsync(512); if (count > 0) { receivedData = reader.ReadString(count); Debug.WriteLine(receivedData); txtblock_showstatus.Text += System.Environment.NewLine; txtblock_showstatus.Text += receivedData; } } catch (Exception exception) { // If this is an unknown status, // it means that the error is fatal and retry will likely fail. if (SocketError.GetStatus(exception.HResult) == SocketErrorStatus.Unknown) { throw; } StatusText = "Receive failed with error: " + exception.Message; Debug.WriteLine(StatusText); // Could retry, but for this simple example // just close the socket. closing = true; clientSocket.Dispose(); clientSocket = null; connected = false; } } 

和我在服务器端的代码是这样的(node.js)

 var net = require('net'); var HOST = '127.0.0.1'; var PORT = 1337; // Create a server instance, and chain the listen function to it // The function passed to net.createServer() becomes the event handler for the 'connection' event // The sock object the callback function receives UNIQUE for each connection net.createServer(function (sock) { // We have a connection - a socket object is assigned to the connection automatically console.log('CONNECTED: ' + sock.remoteAddress + ':' + sock.remotePort); sock.on('data', function (data) { console.log(sock.name + "> " + data, sock); }); // Add a 'close' event handler to this instance of socket sock.on('close', function (data) { console.log('CLOSED: ' + sock.remoteAddress + ' ' + sock.remotePort); sock.end(); }); }).listen(PORT, HOST); 

之前,我改变了node.js代码

 net.createServer(function (sock) { console.log('CONNECTED: ' + sock.remoteAddress + ':' + sock.remotePort); sock.write("Hello"); //}); 

消息“你好”出现在我的客户端正确的问题是,当我添加这些行,代码不再工作。

 sock.on('data', function (data) { console.log(sock.name + "> " + data, sock); }); 

我发送的信息只是一个string。 这似乎是信息不正确的。

 sock.on('data', function (data) {} ); 

无论如何,我可以做这件事情?

谢谢。