2016-02-16 111 views
0

我正在嘗試使用Java Web應用程序來實現簡單的WebSocket程序。Java中的WebSocket編程:客戶端服務器通信問題

但是,無法建立客戶端和服務器之間的通信。

任何人都可以幫助我嗎?

Web服務器:Tomcat的

客戶端代碼: JSP /的javascrip

<body> 
<div> 
    <input type="text" value="" id="message" /> <br /> <input 
     type="submit" value="Start" onclick="start()" /> 
</div> 
<div id="messages"></div> 
<script type="text/javascript"> 
    var webSocket; 
    var uri = 'ws://' + window.location.host + '/ZebraHosting/testwebsocket'; 
    alert('ur url is ' + uri); 
    function connect() { 

     if ('WebSocket' in window) { 
      alert('I am in Websocket in window'); 
      websocket = new WebSocket(uri); 
     } else if ('MozWebSocket' in window) { 
      websocket = new MozWebSocket(uri); 
      alert('I am in MozWebsocket in window'); 
     } else { 
      alert('WebSocket is not supported by this browser.'); 
      return; 
     } 

     webSocket.onerror = function(event) { 
      alert('I am onerror'); 
      onError(event); 
     }; 

     webSocket.onopen = function(event) { 
      alert('I am onopen'); 
      onOpen(event); 
     }; 

     webSocket.onmessage = function(event) { 
      alert('I am onmessage'); 
      onMessage(event); 
     }; 

     webSocket.onclose = function(event) { 
      alert('I am onclose'); 
      onClose(event); 
     }; 

    } 
    function onMessage(event) { 
     document.getElementById('messages').innerHTML += '<br />' 
       + event.data; 
    } 

    function onOpen(event) { 
     alert("function onOpen "); 
     document.getElementById('messages').innerHTML = 'Connection established'; 
    } 

    function onError(event) { 
     alert("Error ocurred "); 
    } 

    function start() { 
     alert("function start "); 
     webSocket.send(document.getElementById('message').value); 
     return false; 
    } 

    function onClose(event) { 
     alert("function onClose"); 
     document.getElementById('messages').innerHTML = 'Connection closed'; 
    } 

    connect(); 
</script> 

服務器代碼:

import java.io.IOException; 
import javax.websocket.OnClose; 
import javax.websocket.OnMessage; 
import javax.websocket.OnOpen; 
import javax.websocket.Session; 
import javax.websocket.server.ServerEndpoint; 

@ServerEndpoint("/testwebsocket") 
public class WebSocketTest { 

@OnMessage 
public void onMessage(String message, Session session) throws IOException, InterruptedException { 

    // Print the client message for testing purposes 
    System.out.println("Received: " + message); 
    // Send the first message to the client 
    session.getBasicRemote().sendText("replay from server for :" + message); 
} 

@OnOpen 
public void onOpen() { 
    System.out.println("Client connected"); 
} 

@OnClose 
public void onClose() { 
    System.out.println("Connection closed"); 
}} 
+0

等什麼日誌都印上客戶端和服務器? – wero

+0

@wero沒有截至目前的日誌,我認爲沒有任何工作後「功能啓動()」 –

+0

我明天會發佈一個工作示例(如果它尚未解決)。 – Gero

回答

3

您的代碼中存在拼寫錯誤:您創建了WebSocket對象並將其分配給變量websocket,但稍後使用變量webSocket

+0

感謝您接收,但仍然沒有輸出。還有一個問題是服務器類是普通的Java類,在這種情況下,我是否需要每次部署war文件?因爲我現在正在做的只是從eclipse執行上述jsp頁面。 –

+0

通過更新Typo webSocket - > websocket,程序正常運行。 –