Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.PrintWriter;
- import java.net.Socket;
- public class ClientSessionTest {
- private Socket socket;
- public ClientSessionTest(Socket socket) {
- System.out.println("new ClientSessionTest()");
- this.socket = socket;
- initClientListener();
- }
- private void initClientListener() {
- System.out.println("initClientListener()");
- Thread t = new Thread(new Runnable() {
- @Override
- public void run() {
- try {
- BufferedReader socketReader = new BufferedReader(
- new InputStreamReader(socket.getInputStream()));
- final PrintWriter socketWriter = new PrintWriter(socket
- .getOutputStream(), true);
- String clientKey = null;
- String responseKey = null;
- while (true) {
- String line = socketReader.readLine();
- if (line == null) {
- System.out
- .println("received null from client - closing connection");
- break;
- } else if (line.isEmpty()) {
- System.out.println("empty line");
- String _01 = "HTTP/1.1 101 Switching Protocols";
- String _02 = "Upgrade: websocket";
- String _03 = "Connection: Upgrade";
- String _04 = "Sec-WebSocket-Accept: " + responseKey;
- String _05 = "Sec-WebSocket-Protocol: chat";
- String _06 = "Content-Encoding: identity";
- System.out.println(_01);
- System.out.println(_02);
- System.out.println(_03);
- System.out.println(_04);
- System.out.println(_05);
- System.out.println(_06);
- System.out.println("");
- socketWriter.println(_01);
- socketWriter.println(_02);
- socketWriter.println(_03);
- socketWriter.println(_04);
- socketWriter.println(_05);
- socketWriter.println(_06);
- socketWriter.println("");
- while (true) {
- byte[] buff = new byte[20];
- int length = socket.getInputStream().read(buff);
- byte[] bstr = new byte[length];
- System.arraycopy(buff, 0, bstr, 0, length);
- System.out.println(new String(bstr));
- for(byte b : bstr){
- System.out.print(((int)b) + " ");
- }
- System.out.println();
- System.out.println();
- }
- } else if (line.startsWith("Sec-WebSocket-Key:")) {
- clientKey = line.replace("Sec-WebSocket-Key: ", "");
- responseKey = ResponseGenerator
- .toResponseKey(clientKey);
- } else {
- System.out.println("" + line);
- // socketWriter.println("lala");
- }
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- });
- t.start();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment