简体   繁体   English

HTML5 WebSocket连接到C#TCP套接字

[英]HTML5 websocket connection to c# tcp socket

I am trying to connect to a C# TCP server using HTML5 from the example in http://www.tutorialspoint.com/html5/html5_websocket.htm 我正在尝试从http://www.tutorialspoint.com/html5/html5_websocket.htm中的示例使用HTML5连接到C#TCP服务器

<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript">
function WebSocketTest()
{
    if ("WebSocket" in window)
    {
     alert("WebSocket is supported by your Browser!");
     // Let us open a web socket
     var ws = new WebSocket("ws://localhost:9998/echo");
     ws.onopen = function()
     {
        // Web Socket is connected, send data using send()
        ws.send("Message to send");
        alert("Message is sent...");
     };
     ws.onmessage = function (evt) 
     { 
        var received_msg = evt.data;
        alert("Message is received...");
     };
     ws.onclose = function()
     { 
        // websocket is closed.
        alert("Connection is closed..."); 
     };
    }
    else
    {
     // The browser doesn't support WebSocket
     alert("WebSocket NOT supported by your Browser!");
    }
}
</script>
</head>
<body>
<div id="sse">
<a href="javascript:WebSocketTest()">Run WebSocket</a>
</div>
</body>
</html>

The TCP server runs on the local machine on port 4530. So I changed TCP服务器在本地计算机上的端口4530上运行。因此我更改了

var ws = new WebSocket("ws://localhost:9998/echo"); 

to

var ws = new WebSocket("ws://localhost:4530");

When I run the page, I get the message WebSocket is supported by your Browser! 当我运行该页面时,我得到消息: 浏览器支持WebSocket! and it hangs there. 它挂在那里。 Any help? 有什么帮助吗?

Mika, have a look at http://xsockets.net , its a easy to get started real-time framework for dotnet (c#) , is canbe found on Nuget. Mika,请看http://xsockets.net ,它可以在Nuget上找到它的易于入门的dotnet(c#)实时框架。 Just hit Install-Package XSockets in the PM Console. 只需在PM控制台中点击Install-Package XSockets。

The framework handels 6455 specifications aswell has it does do the "old" Hybi00 protocl (draft). 框架还处理6455规范,它确实执行“旧的” Hybi00协议(草稿)。

It has a jQuery similar clientside JavaScript library and the server-side API reminds of MVC. 它有一个与jQuery类似的客户端JavaScript库,服务器端API让人想起MVC。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM