在 HTML5 和 C# Web 套接字通信之间
between html5 and C# web socket communication
尝试使用此代码将字符串数据发送到Windows C#应用程序;
try {
var ws = new WebSocket('ws://192.168.1.77:10048');
ws.onopen = function () {
ws.send("Sunucuya mesaj"); // I WANT TO SEND THIS MESSAGE TO SERVER!
};
ws.onclose = function () {
alert('Bağlantı kapandı.');
};
}
catch (e) {
alert(e);
}
并尝试使用此代码从 Windows C# 应用程序获取数据;
static Socket serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
static private string guid = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
serverSocket.Bind(new IPEndPoint(IPAddress.Any, 8181));
serverSocket.Listen(128);
serverSocket.BeginAccept(null, 0, OnAccept, null);
private static void OnAccept(IAsyncResult result)
{
byte[] buffer = new byte[1024];
try
{
Socket client = null;
string headerResponse = "";
if (serverSocket != null && serverSocket.IsBound)
{
client = serverSocket.EndAccept(result);
var i = client.Receive(buffer);
headerResponse = (System.Text.Encoding.UTF8.GetString(buffer)).Substring(0, i);
// write received data to the console
Console.WriteLine(headerResponse);
}
if (client != null)
{
var key = headerResponse.Replace("ey:", "`")
.Split('`')[1] // dGhlIHNhbXBsZSBub25jZQ== 'r'n .......
.Replace("'r", "").Split(''n')[0] // dGhlIHNhbXBsZSBub25jZQ==
.Trim();
// key should now equal dGhlIHNhbXBsZSBub25jZQ==
var test1 = AcceptKey(ref key);
var newLine = "'r'n";
var response = "HTTP/1.1 101 Switching Protocols" + newLine
+ "Upgrade: websocket" + newLine
+ "Connection: Upgrade" + newLine
+ "Sec-WebSocket-Accept: " + test1 + newLine + newLine
//+ "Sec-WebSocket-Protocol: chat, superchat" + newLine
//+ "Sec-WebSocket-Version: 13" + newLine
;
// which one should I use? none of them fires the onopen method
client.Send(System.Text.Encoding.UTF8.GetBytes(response));
var i = client.Receive(buffer); // wait for client to send a message
// once the message is received decode it in different formats
Console.WriteLine(Convert.ToBase64String(buffer).Substring(0, i));
Console.WriteLine("'n'nPress enter to send data to client");
Console.Read();
var subA = SubArray<byte>(buffer, 0, i);
client.Send(subA);
Thread.Sleep(10000);//wait for message to be send
}
}
catch (SocketException exception)
{
throw exception;
}
finally
{
if (serverSocket != null && serverSocket.IsBound)
{
serverSocket.BeginAccept(null, 0, OnAccept, null);
}
}
}
使用此代码,我可以在客户端和服务器之间进行通信,但我无法获得ws.send("Sunucuya mesaj"); 来自从客户端发送的服务器的消息。headerResponse = (System.Text.Encoding.UTF8.GetString(buffer)).子字符串(0, i);在此行中,您可以看到从服务器获取的标头;
获取/HTTP/1.1
升级:网络套接字
连接:升级
主机:192.168.1.77:8181
产地:http://localhost
Sec-WebSocket-Key: dHI34r7feV/Ar4G0/fONCg==
秒网套接字版本:13
Sec-WebSocket-Extensions: x-webkit-deflate-frame
如何从服务器端获取来自客户端的数据?
谢谢。。。
客户端和服务器之间的消息不是纯文本。 有关如何编码/解码消息的详细信息,请参阅协议规范的数据成帧部分。
您可以从几个自由许可的开源 C# 服务器中获取示例。 如弗莱克
相关文章:
- 如何使用HTML5与UDP套接字通信
- 在node.js中写入ECONNRESET错误和套接字连接检查
- java,javascript签名的小程序(文件选择,ftp上传)安全性/套接字异常
- 套接字服务器向客户端广播,但不从两者接收
- 如何确定套接字中的变量是否匹配
- 如何通过网络套接字连接到树莓派上的Mosquitto经纪人
- 在 HTML5 和 C# Web 套接字通信之间
- 是否可以与位于不同于html5网站呈现的服务器的网络套接字进行通信
- 无法与网络套接字通信.高速公路:收到 HELLO 消息,会话尚未建立
- Javascript到Java通过套接字编程进行通信
- 安卓应用和网络浏览器之间的套接字通信
- 客户端Java服务器与Javascript之间的套接字通信
- 如何使用javascript/html5中的web套接字与python通信
- C#Tcp服务器-HTML5网络套接字通信
- Java和JavaScript套接字通信
- 使HTML页面与C中的套接字通信
- 使用网络套接字的低延迟通信&Java服务器
- 套接字.IO客户端无法与服务器通信
- 我可以创建一个java服务器,既与java套接字与web套接字通信
- Nodejs, express &套接字.两个静态客户端页面之间的IO通信