VB6 Winsock Server与WebSocket连接

时间:2016-05-16 09:05:36

标签: websocket vb6 winsock

我有一个用HTML5和Javascript创建的WebSocket,我正在尝试使用VB6中配置的Winsock连接它,WebSocket正在与Winsock通信,而WebSocket连接仍处于Connecting阶段,而且没有连接,似乎我有使用WebSocketm进行握手时出现问题。请帮我查一下我的代码问题

我的vb6代码如下:

Private Sub Form_Load()
    Winsock1.LocalPort = 6701
    Winsock1.Listen
End Sub

Private Sub Winsock1_ConnectionRequest(Index As Integer, ByVal requestID As Long)
    Winsock1(Index).Close
    Winsock1(Index).Accept requestID

    Dim Packet As String

    With Winsock1(Index)

        Packet = "HTTP/1.1 101 Web Socket Protocol Handshake" & vbNewLine
        Packet = Packet & "Upgrade: WebSocket" & vbNewLine
        Packet = Packet & "Connection: Upgrade" & vbNewLine
        Packet = Packet & "Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=" & vbNewLine

        Data = Empty

        .SendData Packet

     End With
End Sub


Private Sub Winsock1_DataArrival(Index As Integer, ByVal bytesTotal As    Long)
    Dim sData As String
    Winsock1(Index).GetData sData
    txtText1.Text = sData
End Sub

并示例WebScocket html

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <script type="text/javascript">
        function connect() {
            var ws = new WebSocket("ws://127.0.0.1:6701");
            ws.onopen = function () {
                console.log('open ');
            };

            ws.onmessage = function (evt) {
                console.log("About to receive data");
                var received_msg = evt.data;
                console.log("Message received = " + received_msg);
            };
            ws.onclose = function () {
                // websocket is closed.
                console.log("Connection is closed...");
            };
        };


    </script>
</head>
<body style="font-size:xx-large" >
    <div>
    <a href="#" onclick="connect()">Click here to start</a></div>
</body>
</html>

0 个答案:

没有答案