【发布时间】:2015-04-25 23:34:13
【问题描述】:
我想通过带有 c# windows store app 客户端的套接字向 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);
});
我发送的消息只是一个字符串。 似乎该消息不正确。
sock.on('data', function (data) {} );
有没有什么办法可以让这件事发挥作用?
谢谢。
【问题讨论】:
标签: javascript c# node.js sockets socket.io