您好,我在創建Java腳本代碼的套接字通信時遇到了問題。從javascript中的套接字獲取信息時出錯
我在發送消息或從服務器關閉套接字時總是出錯。
我的套接字服務器代碼。
// Start listening for connections.
while (true)
{
Console.WriteLine("Waiting for a connection...");
// Program is suspended while waiting for an incoming connection.
Socket handler = listener.Accept();
data = null;
// An incoming connection needs to be processed.
while (true)
{
int bytesRec = handler.Receive(bytes);
data += Encoding.ASCII.GetString(bytes, 0, bytesRec);
break;
}
// Show the data on the console.
Console.WriteLine("Text received : {0}", data);
// Echo the data back to the client.
byte[] msg = Encoding.ASCII.GetBytes(data);
handler.Send(msg);
handler.Shutdown(SocketShutdown.Both);
handler.Close();
}
JavaScript代碼:
var connection = new WebSocket('ws://Myip:11000', ['soap', 'xmpp']);
// When the connection is open, send some data to the server
connection.onopen = function() {
connection.send('Ping'); // Send the message 'Ping' to the server
connection.send('your message');
};
// Log errors
connection.onerror = function (error) {
console.log('WebSocket Error ' + error);
};
connection.onclose = function (msg) {
console.log('WebSocket Error ' + msg);
};
它獲取連接到服務器套接字,但總是得到錯誤,同時關閉或從服務器發送消息。
感謝您的回覆,是的,您是對的,我剛剛開始使用此套接字編程,並且沒有找到任何可靠的來源。如果你能幫助我,我會很高興。 – Rex 2014-10-17 09:25:49