2016-03-21 30 views
1

我正在一个简单的服务器上工作,只是为了了解它是如何工作的。我有一个Java服务器和Java客户端工作,但试图让一个JavaScript客户端连接到同一个Java服务器正在让我疯狂!Javascript客户端无法连接到java服务器

Java服务器:

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.io.PrintWriter; 
import java.net.ServerSocket; 
import java.net.Socket; 

public class DateServer { 

    public static void main(String[] args) throws IOException { 
     ServerSocket listener = new ServerSocket(9091); 
     System.out.println("Starting"); 
     try { 
      while (true) { 
       System.out.println("While"); 
       Socket socket = listener.accept(); 
       System.out.println("Accepted"); 
       BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream())); 
       String msg = input.readLine(); 
       System.out.println("Message from client: " + msg); 
       try { 
        PrintWriter out = 
         new PrintWriter(socket.getOutputStream(), true); 

        System.out.println("Sending message..."); 
        out.println("Hi"); 
       } finally { 
        System.out.println("Closing socket"); 
        socket.close(); 
       } 
      } 
     } 
     finally { 
      listener.close(); 
     } 
    } 
} 

Java客户端:(工作)

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.io.PrintWriter; 
import java.net.Socket; 
import java.net.UnknownHostException; 

import javax.swing.JOptionPane; 

public class DateClient { 

    public static void main(String[] args) throws IOException { 
     Socket s = new Socket("localhost", 9091); 

     try { 
      PrintWriter out = 
       new PrintWriter(s.getOutputStream(), true); 

      System.out.println("Sending message..."); 
      out.println("I'm the client"); 
     } finally { 
      //System.out.println("Closing socket"); 
      //s.close(); 
     } 

     BufferedReader input = new BufferedReader(new InputStreamReader(s.getInputStream())); 
     String msg = input.readLine(); 
     System.out.println(msg); 
     JOptionPane.showMessageDialog(null, msg); 
     s.close(); 
     System.exit(0); 
    } 
} 

JavaScript客户端:

<!DOCTYPE html> 

<html> 
<head> 
     <title>Echo Test</title> 
     <meta charset="UTF-8"> 
     <meta name="viewport" content="width=device-width"> 
    </head> 
<body> 
<div id="messages"></div> 
<script type="text/javascript"> 

var connection = new WebSocket('ws://localhost:9091/'); 
var messages = document.getElementById("messages"); 

// When the connection is open, send some data to the server 
connection.onopen = function (e) { 
    writeResponse('sent'); 
    connection.send('Ping'); 
} 

// Log errors 
connection.onerror = function (error) { 
    writeResponse('Error: ' + error); 
} 

connection.onclose = function(e){ 
    writeResponse("Disconnected: " + e.data); 
} 

// Log messages from the server 
connection.onmessage = function (e) { 
    writeResponse('Server: ' + e.data); 
} 

function writeResponse(text){ 
       messages.innerHTML += "<br/>" + text; 
      } 

</script> 
</body> 
</html> 

当我运行我得到服务器的Java客户端:

Starting 
While 
Accepted 
Message from client: I'm the client 
Sending message... 
Closing socket 
While 
从客户端

输出:

Sending message... 
Hi 

使作品。当我运行JavaScript客户端,服务器的输出是:

Starting 
While 
Accepted 
Message from client: GET/HTTP/1.1 
Sending message... 
Closing socket 
While 

和JavaScript页面上的输出:

Error: [object Event] 
Disconnected: undefined 

此外,火狐控制台说:“火狐无法建立与连接服务器在ws:// localhost:9091 /“。我错过了什么?谢谢!

回答

1

来自浏览器的webSocket连接不仅仅是一个普通的TCP连接。它必须支持完整的webSocket协议,否则浏览器将无法连接到它或交换数据。这个全WebSocket协议包括:

  1. 经由HTTP的初始连接
  2. 升级到WebSocket协议
  3. 用于初始化WebSocket连接
  4. WebSocket的数据包格式
安全程序

你可以在这里阅读更多关于:https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API/Writing_WebSocket_servers

+0

谢谢!该页面指向我写入HTTP响应的正确方向并发送正确的sec-websocket-accept密钥,并且客户端现在连接到服务器:) – Arlo

+0

@Arlo - 仅供参考,现在已有类实现webSocket服务器Java的。您可能只想抓住其中一个,而不是自己重新实施整个协议。 – jfriend00

相关问题