这个简单服务器的客户端是什么样的?如何处理多个连接?

时间:2019-09-20 14:41:30

标签: javascript java websocket

我正尝试使用https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API/Writing_a_WebSocket_server_in_Java提供的信息。

我对javascript客户端感兴趣。但是,我唯一有意义的一行是:

var exampleSocket = new WebSocket("wss://127.0.0.1:8000");

服务器本身的代码是这样的:

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;



public class WebSocket {
        public static void main(String[] args) throws IOException, NoSuchAlgorithmException {
                ServerSocket server = new ServerSocket(8000);
                try {
                        System.out.println("Server has started on 127.0.0.1:8000.\r\nWaiting for a connection...");
                        Socket client = server.accept();
                        System.out.println("A client connected.");

                        InputStream in = client.getInputStream();
                        OutputStream out = client.getOutputStream();
                        Scanner s = new Scanner(in, "UTF-8");
                        try {
                                String data = s.useDelimiter("\\r\\n\\r\\n").next();
                                Matcher get = Pattern.compile("^GET").matcher(data); //MUST BE DONE IN THIS WAY
                                //developer.mozilla.org "Writing a Websocket Server in Java"
                                if (get.find()) {
                                        Matcher match = Pattern.compile("Sec-WebSocket-Key: (.*)").matcher(data);
                                        match.find();
                                        byte[] response = ("HTTP/1.1 101 Switching Protocols\r\n"
                                                + "Connection: Upgrade\r\n"
                                                + "Upgrade: websocket\r\n"
                                                + "Sec-WebSocket-Accept: "
                                                + Base64.getEncoder().encodeToString(MessageDigest.getInstance("SHA-1").digest((match.group(1) + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11").getBytes("UTF-8")))
                                                + "\r\n\r\n").getBytes("UTF-8");
                                        out.write(response, 0, response.length);

byte[] decoded = new byte[6];
                                        byte[] encoded = new byte[] { (byte) 198, (byte) 131, (byte) 130, (byte) 182, (byte) 194, (byte) 135 };
                                        byte[] key = new byte[] { (byte) 167, (byte) 225, (byte) 225, (byte) 210 };
                                        for (int i = 0; i < encoded.length; i++) {
                                                decoded[i] = (byte) (encoded[i] ^ key[i & 0x3]);
                                        } //else System.println("No Find");
System.out.println("should be decoded");
                                } else System.out.println("No Find");
                        } finally {
                                s.close();
                        }
                } finally {
                        server.close();
                }
        }

但是,我得到的最后一行都是“找不到”。客户端除了“ wss”以外,还需要做什么以使握手正确加密?

我试图通过“打印” s中的数据来理解这一点,但是除了通用信息外,它似乎不包含任何其他内容;由于解密由于某种原因而失败,因此if get.find()行似乎永远不会返回true!

此外,这似乎只需要握手然后退出。一般来说,您可能会建议使用什么方法,以便服务器可以“环回”,以便服务器可以记住多个客户端及其输入/输出流?预先感谢

0 个答案:

没有答案