使用Java服务器时,Onmessage甚至不会触发



我知道这之前已经问过了,但我已经尝试了这个网站上的所有帖子,仍然没有修复,所以我想我可能会上传我的代码,看看我做错了什么。

我使用java为我的服务器和javascript websocket为我的客户端。当我从服务器向客户端发送信息时,什么也没有发生,当我从客户端向服务器发送数据时,什么也没有收到。请帮助? ! ? ! ? !

这是我的服务器代码:

import java.io.*;
import java.net.*;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.*;
import java.awt.*;
import javax.swing.*;
import org.apache.commons.codec.binary.Base64;
public class Server extends JFrame
{
private JTextArea textArea = new JTextArea();
private Scanner fromClient;
private BufferedOutputStream toClient;
private Socket socket;
private static final int TEXT = 1, BINARY = 2, CLOSE = 8, PING = 9, PONG = 10;
public static void main(String[] args)
{
    new Server();
}
public Server()
{
    setLayout(new BorderLayout());
    add(new JScrollPane(textArea), BorderLayout.CENTER);
    setTitle("Server");
    setSize(700, 400);
    setLocation(0, 200);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);
    try 
    {
        //create server socket
        ServerSocket serverSocket = new ServerSocket(8181);
        textArea.append( "Starting Server...n" );
        textArea.append("Server started at " + new Date() + 'n');
        //Wait for connection
        socket = serverSocket.accept();
        //create input output
        fromClient = new Scanner(socket.getInputStream());
        toClient = new BufferedOutputStream(socket.getOutputStream());
        this.handle_handshake(fromClient);
        textArea.append("nnsending text....n");
        //this.brodcast("pop");
        this.send_message("test");
        int i = 1; 
        while (true)
        {
            String s = fromClient.hasNext() ? fromClient.nextLine() : "";
            textArea.append("Client: " + s + "n");
            if( i == 0 ) break;
        }
        socket.close();
        serverSocket.close();
    }
    catch (IOException ex)
    {
        System.err.println(ex);
    }
}
private boolean handle_handshake( Scanner scanner ) 
{
    String line;
    String hash_str = "";
    String key = "";

    int counter = 0;
    while ( scanner.hasNextLine() && (line = scanner.nextLine() ) != null ) 
    {
        String[] tokens = line.split( ": " );

        switch ( tokens[0] ) 
        {
            case "Sec-WebSocket-Key":
                key = tokens[1].trim();
                textArea.append( "SocketKey" );
                hash_str = this.get_return_hash( tokens[1] );
                break;
            case "Sec-WebSocket-Version":
                textArea.append( "WebSock-Ver: " + tokens[1] + "n" );
                break;
            default:
                textArea.append( tokens[0] + ": " + tokens[tokens.length-1] + "n" );
                break;
        }
        ++counter;
        if ( counter > 12 ) 
        {
            textArea.append( "Handshake" );
            String msg = "HTTP/1.1 101 Switching Protocolsrn";
            msg += "Upgrade: websocketrn";
            msg += "Connection: Upgradern";
            msg += "Sec-WebSocket-Accept: " + hash_str + "rn";
            msg += "rnrn";
            try
            {
                toClient.write( msg.getBytes( "UTF-8" ) );
                textArea.append( "Server > Client: " + msg );
                //toClient.flush();
            }
            catch (UnsupportedEncodingException e) 
            {
                e.printStackTrace();
            } 
            catch (IOException e) 
            {
                e.printStackTrace();
            }
            textArea.append( "nnClosing handshakenn" );
            return true;
        }
    }
    return false;
}
public void brodcast(String mess) throws IOException
{
    byte[] rawData = mess.getBytes();
    int frameCount  = 0;
    byte[] frame = new byte[10];
    frame[0] = TEXT;
    if(rawData.length <= 125){
        frame[1] = (byte) rawData.length;
        frameCount = 2;
    }else if(rawData.length >= 126 && rawData.length <= 65535){
        frame[1] = (byte) 126;
        byte len = (byte) rawData.length;
        frame[2] = (byte)((len >> 8 ) & (byte)255);
        frame[3] = (byte)(len & (byte)255); 
        frameCount = 4;
    }else{
        frame[1] = (byte) 127;
        byte len = (byte) rawData.length;
        frame[2] = (byte)((len >> 56 ) & (byte)255);
        frame[3] = (byte)((len >> 48 ) & (byte)255);
        frame[4] = (byte)((len >> 40 ) & (byte)255);
        frame[5] = (byte)((len >> 32 ) & (byte)255);
        frame[6] = (byte)((len >> 24 ) & (byte)255);
        frame[7] = (byte)((len >> 16 ) & (byte)255);
        frame[8] = (byte)((len >> 8 ) & (byte)255);
        frame[9] = (byte)(len & (byte)255);
        frameCount = 10;
    }
    int bLength = frameCount + rawData.length;
    byte[] reply = new byte[bLength];
    int bLim = 0;
    for(int i=0; i<frameCount;i++){
        reply[bLim] = frame[i];
        bLim++;
    }
    for(int i=0; i<rawData.length;i++){
        reply[bLim] = rawData[i];
        bLim++;
    }

    toClient.write(reply);
    toClient.flush();
    textArea.append("nnnEnd of brodcasting: " + mess + "nnn");
}
private void send_message( String msg ) 
{   
    try 
    {
        textArea.append( "Sending Message: " + msg + "n" );
        byte[] textBytes = msg.getBytes( "UTF-8" );
        //ByteArrayOutputStream bao = new ByteArrayOutputStream();
        byte code = TEXT;
        //opcode
        toClient.write( code );
        //length
        toClient.write( (byte) textBytes.length );
        //data
        toClient.write( textBytes );
        //fin end line
        toClient.write( code );
        toClient.flush();
    }
    catch ( IOException e )
    {
        System.out.printf( "IOE: %s", e.getMessage() ); 
    }
}
private String get_return_hash( String key ) 
{
    textArea.append( "|| " + key + " ||n"  );
    String protocol_str = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
    byte[] sha1_bytes = sha1( key + protocol_str );
    String final_hash = new String(Base64.encodeBase64( sha1_bytes ));
    return final_hash;    
}
private byte[] sha1(String input) 
{
    try
    {
        MessageDigest mDigest = MessageDigest.getInstance("SHA1");
        byte[] result = mDigest.digest(input.getBytes());
        return result;
    } 
    catch ( NoSuchAlgorithmException e ) 
    {
        return null;
    }
}
}
下面是我的客户端代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" 
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript" src="assets/jquery-1.9.1.min.js"></script>
<script type="text/javascript">
$( document ).ready( function() 
{
    var ws;
    var host = 'ws://localhost:8181';//'ws://echo.websocket.org';
    if('WebSocket' in window)
    {
        console.log( "Connect" );
        connect(host);
    }
    function connect(host) 
    {
        ws = new WebSocket(host);
        ws.binaryType = 'blob'; //ws.binaryType = 'arraybuffer';
        ws.onopen = function( evt ) 
        {
            console.log( "Connection opened" );
            console.log( "readyState: " + ws.readyState );
            console.log( evt );
            console.log('protocol: ' + ws.protocol);
            setInterval(test, 5000);
        };
        ws.onerror = function( evt ) 
        {
            console.log( 'Error Code: ' + evt.code );
            console.log( evt );
        };
        ws.onmessage = function (evt) 
        {  
            console.log('reveived data:' + evt.data);
        };
        ws.onclose = function (evt) 
        {
            console.log( "Socket closed" );
            console.log( "Reason: " + evt.reason + " Code: " + evt.code );
            console.log( "Clean Close: " + evt.wasClean );
        };
    };
    function test()
    {
        console.log("waiting...");
        //var data = str2ab("Ping");
        var line = 'waiting...';
        // perform some operations on the ArrayBuffer
        console.log( ws.readyState );
        if (ws.readyState == 1) ws.send(line);
        if (ws.bufferedAmount === 0) 
        {
          console.log('SENT!');
        }
        else 
        {
          console.log('NOT SENT! left over' + ws.bufferedAmount);
        }
    };
});
</script>
</head>
<body>
</body>
</html>

谢谢你的帮助!!

您可以解决的最大(或者至少是第一个)问题是在句柄握手中。

改变:

msg += "Sec-WebSocket-Accept: " + hash_str + "rn";

:

msg += "Sec-WebSocket-Accept: " + hash_str;

这会导致握手以三个"rn"结束。客户端在读取前两个之后结束握手,导致第三个留在流中。这意味着您的最后一个rn将作为客户端接下来尝试读取的任何数据的一部分被读入。如果您尝试向客户端发送消息,它将由于错误的操作码(很可能)而断开连接。

相关内容

  • 没有找到相关文章

最新更新