2016-07-26 18 views
0

こんにちは、みんな!WebSocket接続に失敗しました:WebSocketハンドシェイク中にエラーが発生しました:予期しない応答コード:200

ChromeブラウザとGlassfish 4.1.1サーバーの間にwebsocket接続を作成しようとしています。そして4日は成功しません。

"WebSocket connection to 'ws://DOMAIN_NAME:8080/serverendpointdemo/' failed: 
Error during WebSocket handshake: Unexpected response code: 200" 

クライアント側コード:

var ws = new WebSocket ("ws://" + document.location.host + "/serverendpointdemo/"); 

サーバー側のコード:たぶん

package com.za.tutorial.websocket; 

import javax.websocket.OnClose; 
import javax.websocket.OnError; 
import javax.websocket.OnMessage; 
import javax.websocket.OnOpen; 
import javax.websocket.server.ServerEndpoint; 

@ServerEndpoint("/home/USER_NAME/glassfish4/glassfish/domains/domain1/applications/serverendpointdemo") 
public class ServerEndpointDemo { 
    @OnOpen 
    public void handleOpen() { 
     System.out.println("Client is now connected..."); 
    } 

    @OnMessage 
    public String handleMessage (String message) { 
     return null; 
    } 

    @OnClose 
    public void handleClose() { 
    } 

    @OnError 
    public void handleError (Throwable t) { 
     t.printStackTrace(); 
    } 
} 

は残念ながら、私の代わりに101

クロームDevToolログのHTTP 200メッセージが表示されますこの質問に私を助けるためにあなたに何か他のものが必要ですか? 助けてください。

+0

は、私はそれをこのように獲得しました。 参考リンク、elseoneはそれを得た場合: [リンク](http://blog.martinandersson.com/making-a-java-ee-7-websocket-serverendpoint-class-discoverable/) [リンク] (https://glassfish.java.net/docs/4.0/error-message-reference.pdf) [link](http://download.oracle.com/otn-pub/jcp/websocket-1_0-fr) -eval-spec/JavaWebSocketAPI_1.0_Final.pdf?AuthParam = 1469574317_cf28b611d89626ae8c534e5f783409ca) – Rinat

+0

これはGlassfishサーバーの機能です。 GlassFishでは、localhostのポート8080で待機しているWebコンテナにコンテキストルートmycontextrootを配置してアプリケーションをデプロイすると、ws:// localhost:8080/mycontextroot/helloを使用してWebSocketにアクセスできます。本文:[link](http://www.oracle.com/technetwork/articles/java/jsr356-1937161.html) – Rinat

答えて

0

サーバーサイド: @ServerEndpoint(値= "/ WS") パブリッククラスWebSocketConfigを{

private String message = ""; 
private String to = ""; 
private String from = ""; 
private static final Set<Session> sessions = Collections.synchronizedSet(new HashSet<Session>()); 

@OnOpen 
public void onOpen(Session session, EndpointConfig econfig) throws IOException {   
    System.out.println("New Web Socket Connection created"); 
    session.getBasicRemote().sendText("onOpen new socket connection");    
} 

@OnClose 
public void onClose(Session session) {  
     sessions.remove(session); 
} 

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

} 

クライアント側:

=が、新しい接続は、あなたがのcon​​textPath VAR WSを指定する必要がありながらWebSocket( "ws://" + document.location.host + "/" +あなたのcontextPathName + "/ serverendpointdemo /");

ます。http:WS://ホスト名:ポート/のcontextPath/serverendpoint ます。https:WSS://ホスト名:ポート/のcontextPath/serverendpoint

例: WS://document.location.host+ "/ WebsocketDemo/ws "; クライアント側Javascriptコード: するvar WS =新しいのWebSocket( "WS:// DOMAIN_NAME:8080/serverendpointdemo/serverendpointdemo")

+0

この回答が現在の問題を解決する際にOPの助けとなる –

関連する問題