WebSocket」タグアーカイブ

JavaFXとWebsocketを連携してみる(On Glassfish4.0) その2

前回からスレッド周りの制約事項にかかってうまくいっていないけど状況の整理。

やろうとしていることは、前回は表の中にWebsockeで取得したデータを反映しようとしていたものを、Labelに変更して表示しようというもの。

1

起きているエラーはJavaFXのコンポーネントをJavaFX配下以外のスレッドで呼んではいけないというもの。

スタックトレースは以下。

java.lang.IllegalStateException: Not on FX application thread; currentThread = Grizzly-worker(1)
エラーです:e9e8fea3-96b6-46be-ad8e-a393e1499784
    at com.sun.javafx.tk.Toolkit.checkFxUserThread(Toolkit.java:237)
    at com.sun.javafx.tk.quantum.QuantumToolkit.checkFxUserThread(QuantumToolkit.java:397)
    at javafx.scene.Parent$1.onProposedChange(Parent.java:245)
    at com.sun.javafx.collections.VetoableObservableList.setAll(VetoableObservableList.java:90)
    at com.sun.javafx.collections.ObservableListWrapper.setAll(ObservableListWrapper.java:314)
    at com.sun.javafx.scene.control.skin.LabeledSkinBase.updateChildren(LabeledSkinBase.java:602)
    at com.sun.javafx.scene.control.skin.LabeledSkinBase.handleControlPropertyChanged(LabeledSkinBase.java:209)
    at com.sun.javafx.scene.control.skin.SkinBase$3.changed(SkinBase.java:282)
    at javafx.beans.value.WeakChangeListener.changed(WeakChangeListener.java:107)
    at com.sun.javafx.binding.ExpressionHelper$SingleChange.fireValueChangedEvent(ExpressionHelper.java:196)
    at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:100)
    at javafx.beans.property.StringPropertyBase.fireValueChangedEvent(StringPropertyBase.java:121)
    at javafx.beans.property.StringPropertyBase.markInvalid(StringPropertyBase.java:128)
    at javafx.beans.property.StringPropertyBase.access$100(StringPropertyBase.java:67)
    at javafx.beans.property.StringPropertyBase$Listener.invalidated(StringPropertyBase.java:236)
    at com.sun.javafx.binding.ExpressionHelper$SingleInvalidation.fireValueChangedEvent(ExpressionHelper.java:155)
    at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:100)
    at javafx.beans.property.StringPropertyBase.fireValueChangedEvent(StringPropertyBase.java:121)
    at javafx.beans.property.StringPropertyBase.markInvalid(StringPropertyBase.java:128)
    at javafx.beans.property.StringPropertyBase.set(StringPropertyBase.java:161)
    at javafx.beans.property.StringPropertyBase.set(StringPropertyBase.java:67)
    at jp.co.epea.firstfxclient.ObjData.setDataValue(ObjData.java:26)
    at jp.co.epea.firstfxclient.WSClient.onMessage(WSClient.java:30)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.glassfish.tyrus.core.AnnotatedEndpoint.callMethod(AnnotatedEndpoint.java:431)
    at org.glassfish.tyrus.core.AnnotatedEndpoint.access$100(AnnotatedEndpoint.java:83)
    at org.glassfish.tyrus.core.AnnotatedEndpoint$WholeHandler$1.onMessage(AnnotatedEndpoint.java:518)
    at org.glassfish.tyrus.core.SessionImpl.notifyMessageHandlers(SessionImpl.java:391)
    at org.glassfish.tyrus.core.EndpointWrapper.onMessage(EndpointWrapper.java:498)
    at org.glassfish.tyrus.container.grizzly.GrizzlyClientSocket.onMessage(GrizzlyClientSocket.java:393)
    at org.glassfish.tyrus.websockets.frametypes.TextFrameType.respond(TextFrameType.java:66)
    at org.glassfish.tyrus.websockets.DataFrame.respond(DataFrame.java:102)
    at org.glassfish.tyrus.container.grizzly.WebSocketFilter.handleRead(WebSocketFilter.java:300)
    at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:288)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:206)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:136)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:114)
    at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
    at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:837)
    at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:113)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:565)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:545)
    at java.lang.Thread.run(Thread.java:724)

 

現在の書き方(ソース)でかかっている場所は、ClientManager#connectToServerでGrizzlyのスレッドがたっていて、

        @Override
        protected Task createTask() {
            Task<Void> task = new Task<Void>() {
                @Override
                protected Void call() throws Exception {

                    try {
                        URI clientURI = new URI("ws://localhost:8080/first/hello/");
                        ClientManager cliContainer = org.glassfish.tyrus.client.ClientManager.createClient();
                        cliContainer.connectToServer(new WSClient(data), clientURI);

                    } catch (DeploymentException  ex) {
                        Logger.getLogger(SampleController.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    return null;
                }
            };
            return task;
        }

その中で動いているOnmessegeで値を書き換えようとしていること

    ObjData data;

    @OnMessage
    public void onMessage(String message) {
        if (message.length() == 0) {
            return;
        }
        System.out.println(data.getDataValue());
        data.setDataValue(message);//下の自分で作ったメソッド
        System.out.println(data.getDataValue());
    }

// ObjDataの中
    private SimpleStringProperty dataValue;
    public void setDataValue(String dataValue) {
        dataValueProperty().set(dataValue);
    }

改めて前回サンプルにしたソースを見直すとObservableListの変更をトリガーにした通知機能をうまく使っているみたい。

なお、自前でスレッドを管理している場合のドキュメントはこちら

JavaFX&Websocket連携(エラーパターン)

対向のサーバが落ちているとき。

素直にハンドシェイクできないという。まぁ、そのまんま。

7 20, 2013 5:43:29 午後 jp.co.epea.firstfxclient.SampleController$TwitterCheckService$1 call
SEVERE: null
javax.websocket.DeploymentException: Handshake response not received.
    at org.glassfish.tyrus.client.ClientManager.connectToServer(ClientManager.java:300)
    at org.glassfish.tyrus.client.ClientManager.connectToServer(ClientManager.java:172)
    at jp.co.epea.firstfxclient.SampleController$TwitterCheckService$1.call(SampleController.java:65)
    at jp.co.epea.firstfxclient.SampleController$TwitterCheckService$1.call(SampleController.java:1)
    at javafx.concurrent.Task$TaskCallable.call(Task.java:1259)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
    at java.util.concurrent.FutureTask.run(FutureTask.java:166)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:724)

 

URI clientURI = new URI(“ws://localhost:8080/first/hellohoge/”);

みたいにURLが間違っている場合。

Handshake errorというやつとスタックとレースにレスポンスコードが返ってくる。

この例だと存在しないパスなので404が返っているけど、WebsocketじゃないただのHTMLだとUpdate Protocolの時に失敗しているので(?)200が返ってくる。

なお、パスの指定時は最後の”/(スラッシュ)”も厳密に見る模様。

7 20, 2013 8:46:51 午後 jp.co.epea.firstfxclient.SampleController$TwitterCheckService$1 call
SEVERE: null
javax.websocket.DeploymentException: Handshake error.
    at org.glassfish.tyrus.client.ClientManager.connectToServer(ClientManager.java:285)
    at org.glassfish.tyrus.client.ClientManager.connectToServer(ClientManager.java:172)
    at jp.co.epea.firstfxclient.SampleController$TwitterCheckService$1.call(SampleController.java:65)
    at jp.co.epea.firstfxclient.SampleController$TwitterCheckService$1.call(SampleController.java:1)
    at javafx.concurrent.Task$TaskCallable.call(Task.java:1259)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
    at java.util.concurrent.FutureTask.run(FutureTask.java:166)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:724)
Caused by: org.glassfish.tyrus.websockets.HandshakeException: Response code was not 101: 404
    at org.glassfish.tyrus.websockets.HandShake.validateServerResponse(HandShake.java:314)
    at org.glassfish.tyrus.websockets.draft06.HandShake06.validateServerResponse(HandShake06.java:98)
    at org.glassfish.tyrus.container.grizzly.WebSocketFilter.handleClientHandShake(WebSocketFilter.java:368)
    at org.glassfish.tyrus.container.grizzly.WebSocketFilter.handleHandshake(WebSocketFilter.java:353)
    at org.glassfish.tyrus.container.grizzly.WebSocketFilter.handleRead(WebSocketFilter.java:274)
    at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:288)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:206)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:136)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:114)
    at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
    at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:837)
    at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:113)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:565)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:545)
    ... 1 more

Connection had closed.

 

ちなみに、エラーログをはいているのはSampleController#createTaskの中にあるキャッチ節。

        @Override
        protected Task createTask() {
            Task<Void> task = new Task<Void>() {
                @Override
                protected Void call() throws Exception {
                    messageLatch = new CountDownLatch(1);
                    try {
                        URI clientURI = new URI("ws://localhost:8080/first/hellohoge/");
                        ClientManager cliContainer = org.glassfish.tyrus.client.ClientManager.createClient();
                        cliContainer.connectToServer(new TwitterClient(table), clientURI);
                        messageLatch.await(1, TimeUnit.SECONDS);
                    } catch (DeploymentException | URISyntaxException | InterruptedException ex) {
                        Logger.getLogger(SampleController.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    return null;
                }
            };
            return task;
        }

下の@OnErrorは通らずに、@OnClose(前のログだと緑字部分)で終わるので注意。

    @OnClose
    public void closeConnection(Session session) {
        System.out.println("Connection had closed.");
    }

    @OnError
    public void onError(Session session,Throwable t) {
        System.out.println("error");
        t.printStackTrace();
    }

javafx.concurrent.Taskの使い方次第でもっとハンドリングできそうな気がする。
とりあえず参考になりそうなサイト

JavaFXとWebsocketを連携してみる(On Glassfish4.0) その1

サーバサイドはいつものようにGlassfish 4.0。まずは寺田さんのブログを見つつメッセージ送信用のページで入れたメッセージが表示されるようにするとこまで持っていく。(元のブログのようにツイッターを見に行きはしないで、index.htmlで入れたものが表示されるようにする。)

サーバ用のエンドポイント(ws://localhost:8080/first/hello/)

package jp.co.epea.first;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

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

@ServerEndpoint("/hello/")
public class HelloWorld {

    static Set<Session> sessions = Collections
            .synchronizedSet(new HashSet<Session>());

    @OnMessage
    public void onMessage(String message) {
        System.out.println("message[" + message + "]");

        for (Session s : sessions) {
            s.getAsyncRemote().sendText(message);
        }
    }

    @OnOpen
    public void open(Session sess) {
        System.out.println("開始します:" + sess.getId());
        sessions.add(sess);
    }

    @OnClose
    public void close(Session sess) {
        System.out.println("終了します:" + sess.getId());
        sessions.remove(sess);
    }

    @OnError
    public void error(Session sess,Throwable t) {
        System.out.println("エラーです:" + sess.getId());
        t.printStackTrace();
    }
}

メッセージ送信用のページ(http://localhost:8080/first/index.html)

<!DOCTYPE html>
<html>
    <head>
        <title>WebSocketテスト</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
        <script type="text/javascript">
            var socket;
            $(document).ready(function(){
                var host="ws://localhost:8080/first/hello/";
                socket = new WebSocket(host);

                socket.onmessage = function(message){
                    $('#log').append(message.data + "<br/>");
                }

                $('#send').click(function(){
                    var text = $('#msg').val();
                    socket.send(text);
                    $('#msg').val('');
                })

            });
        </script>        
    </head>
    <body>
        <h1>WebSocketテスト</h1>
        <div id="log">
        </div>
        <input id="msg" type="text"/>
        <button id="send">送信</button>
    </body>
</html>

 

クライアントサイドの作成の流れは

  1. MavanプロジェクトとしてJavaFXを作成(前回ブログ)
  2. 足りない依存関係を追加
  3. 寺田さんのブログのクライアントサイドプログラムをコピペ
  4. とりあえず動くようになるために最低限の修正を加える

(手順1と3は省略)

2.足りない依存関係を追加

tyrus-clientとtyrus-container-grizzlyはこちらのブログを参考にしたときに「Grizzly 関連の jar」の今版として使えそうなので試したところいけたっぽいので持ってきた。(ユーザガイド)

    <dependencies>
        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>javafx</artifactId>
            <version>2.0</version>
            <scope>system</scope>
            <systemPath>C:\dev\pleiades43\java\7\lib/jfxrt.jar</systemPath>
        </dependency>
        <dependency>
            <groupId>org.glassfish.tyrus</groupId>
            <artifactId>tyrus-client</artifactId>
            <version>1.1</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.glassfish.tyrus</groupId>
            <artifactId>tyrus-container-grizzly</artifactId>
            <version>1.1</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>

 

4.とりあえず動くようになるために最低限の修正を加える

TwitterClientのアノテーションを@ClientEndpoint、@OnOpen、@OnMessage、@OnCloseに変更。

SampleController.javaのURLを修正。

ClientEndpointConfigurationの行をコメントアウト。

URI clientURI = new URI("ws://localhost:8080/first/hello/");
//ClientEndpointConfiguration clientConfig = new DefaultClientConfiguration();

 

動作確認

サーバを起動する。

JavaFXを起動する。

a

start timelineボタンをクリックしてサーバと接続

(コンソールメッセージ)
Connection had opened.

 

index.htmlからメッセージを送信

b

 

JavaFX側に表示される

c

 

一応これで動作しているようなのでこいつを基点に触っていくことにする。

(JavaFX側をXボタンで閉じたときにOncloseが呼ばれてないっぽいけど、実装的にそういう動きになるのか環境がおかしいのか。。。。。)

2013/11/03追記

子の実装ではJavaFXのTableViewを使用することで、WebSocket側のスレッドによる変更がJavaFXのスレッドに通知されている。(オブザーバ)

桜庭さんに伺ったところ、「(GUIはそっちのスレッドから動かすのが基本なので)きちんと動かないことあるかもよ」とのことでした。

他の日記ではLabelで同期しようとした場合にPlatform.runLaterでJavaFXのスレッドに処理を戻しているけど、そちらの方法が基本とのことでした。

 

WebSocketを使ってみる。その4

続けて@PathParamを試してみる。
APIによると
ServerEndpointに{パラメータ}で指定した値とバインドしてくれるらしい。

変数として使える型はStringかプリミティブOrそのボクシング型とのこと。

ただし、パラメータがデコードできなかったらエラーハンドラーを呼び出すらしい。

Java (zip:paramBindTest)

import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;

@ServerEndpoint("/hello/{hoge}/{fuga}")
public class HelloWorld {

    static Set<Session> sessions = Collections
            .synchronizedSet(new HashSet<Session>());

    @OnMessage
    public void onMessage(String message,
           @PathParam("hoge") Integer hoge,@PathParam("fuga") String fuga) {
        System.out.println("message[" + message + "]");
        System.out.println("hoge[" + hoge + "]");
        System.out.println("fuga[" + fuga + "]");
        for (Session s : sessions) {
            s.getAsyncRemote().sendText(message);
        }
    }

JS

            $(document).ready(function(){
                var host="ws://localhost:8080/first/hello/12/ああ";
                socket = new WebSocket(host);

                socket.onmessage = function(message){
                    $('#log').append(message.data + "<br/>");
                }

結果

情報: 開始します:8ca15fe1-a79d-420e-afc6-3c5ab18a75ec
情報: message[あsdf]
情報: hoge[12] <- ボクシング型にバインドされている
情報: fuga[ああ] <- 日本語も素直にバインドされる模様

日本語もそのままうまくバインドされた。

ためしにIntegerの部分を少数にしたらNumberFormatExceptionをチェーンしたDecodeExceptionが発生。このケースは切り分け難しくない。

情報: エラーです:9366a9f2-2b24-4be3-8897-40427563d56e
重大: javax.websocket.DecodeException: Decoding failed
    at org.glassfish.tyrus.core.PrimitiveDecoders$IntegerDecoder.decode(PrimitiveDecoders.java:165)
    at org.glassfish.tyrus.core.PrimitiveDecoders$IntegerDecoder.decode(PrimitiveDecoders.java:157)
    at org.glassfish.tyrus.core.AnnotatedEndpoint$2.value(AnnotatedEndpoint.java:362)
    at org.glassfish.tyrus.core.AnnotatedEndpoint.callMethod(AnnotatedEndpoint.java:428)
    at org.glassfish.tyrus.core.AnnotatedEndpoint.access$100(AnnotatedEndpoint.java:83)
    at org.glassfish.tyrus.core.AnnotatedEndpoint$WholeHandler$1.onMessage(AnnotatedEndpoint.java:518)
    at org.glassfish.tyrus.core.SessionImpl.notifyMessageHandlers(SessionImpl.java:389)
    at org.glassfish.tyrus.core.EndpointWrapper.onMessage(EndpointWrapper.java:495)
    at org.glassfish.tyrus.server.TyrusEndpoint.onMessage(TyrusEndpoint.java:174)
    at org.glassfish.tyrus.websockets.DefaultWebSocket.onMessage(DefaultWebSocket.java:156)
    at org.glassfish.tyrus.websockets.frametypes.TextFrameType.respond(TextFrameType.java:66)
    at org.glassfish.tyrus.websockets.DataFrame.respond(DataFrame.java:102)
    at org.glassfish.tyrus.servlet.TyrusHttpUpgradeHandler.onDataAvailable(TyrusHttpUpgradeHandler.java:113)
    at org.apache.catalina.connector.InputBuffer$ReadHandlerImpl.processDataAvailable(InputBuffer.java:488)
    at org.apache.catalina.connector.InputBuffer$ReadHandlerImpl.onDataAvailable(InputBuffer.java:453)
    at org.glassfish.grizzly.http.io.InputBuffer.append(InputBuffer.java:855)
    at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:222)
    at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:288)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:206)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:136)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:114)
    at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
    at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:838)
    at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:113)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:564)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:544)
    at java.lang.Thread.run(Thread.java:724)
Caused by: java.lang.NumberFormatException: For input string: "1.2"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:492)
    at java.lang.Integer.valueOf(Integer.java:582)
    at org.glassfish.tyrus.core.PrimitiveDecoders$IntegerDecoder.decode(PrimitiveDecoders.java:163)
    ... 30 more

 

nullとした場合も素直にエラー

var host="ws://localhost:8080/first/hello/null/ああ";
->
Caused by: java.lang.NumberFormatException: For input string: "null"

空文字だと、予想外に素直なエラー

var host="ws://localhost:8080/first/hello//ああ";
->
Caused by: java.lang.NumberFormatException: For input string: ""

 

最後の文字列が空文字だと空文字としてバインドされる。

var host="ws://localhost:8080/first/hello/12/";
->
情報: 開始します:75b4bc74-30e9-4a69-8af3-07ed67e6a296
情報: message[あ]
情報: hoge[12]
情報: fuga[]

/{空文字}/{数字}の順でも動いた。(//となるので環境次第では不具合が出る??

@ServerEndpoint("/hello/{fuga}/{hoge}")と最初を文字列にする
->
var host="ws://localhost:8080/first/hello//12";
->
情報: 開始します:028c5db1-a60f-4d82-970f-8933058b2bfb
情報: message[あああ]
情報: hoge[12]
情報: fuga[]

{変数名}と@PathParamで誤記をするとバインドされない(null)。

@ServerEndpoint("/hello/{fuga}/{hoge}")
->
public void onMessage(String message,
  @PathParam("hoge") Integer hoge,
  @PathParam("fuga2") String fuga)
->
情報: 開始します:49d01de4-4a6b-4b81-8c71-d6d48d9f0600
情報: message[あ]
情報: hoge[12]
情報: fuga[null]

 

URLパラーンの違いによっては呼び出されない。

一つ前のnullがバインドされる事象はバインド対象が不整合でも動いていたけど、そっちが中途半端に動いているだけで、呼び出されないのが正しいのかな??

@ServerEndpoint("/hello/{hoge}") <-fuga2はない
->
public void onMessage(String message,
   @PathParam("hoge") Integer hoge,]
   @PathParam("fuga2") String fuga) {

->
23:27:25.244[23ms][total 23ms] Status: 404[Not Found]
GET http://localhost:8080/first/hello/12/nasi Load Flags[LOAD_BYPASS_CACHE  LOAD_BACKGROUND  INHIBIT_CACHING  ] Content Size[-1] Mime Type[application/x-unknown-content-type]
   Request Headers:
      Host[localhost:8080]
      User-Agent[Mozilla/5.0 (Windows NT 6.1; WOW64; rv:22.0) Gecko/20100101 Firefox/22.0]
      Accept[text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8]
      Accept-Language[ja,en-us;q=0.7,en;q=0.3]
      Accept-Encoding[gzip, deflate]
      Sec-WebSocket-Version[13]
      Origin[http://localhost:8080]
      Sec-WebSocket-Key[gq8u2S2nXT2xpIagf7qPaQ==]
      Cookie[JSESSIONID=e1e94c1a637e58c7ccaa61c6f17e]
      Connection[keep-alive, Upgrade]
      Pragma[no-cache]
      Cache-Control[no-cache]
      Upgrade[websocket]
   Response Headers:
      X-Powered-By[Servlet/3.1 JSP/2.3 (GlassFish Server Open Source Edition  4.0  Java/Oracle Corporation/1.7)]
      Server[GlassFish Server Open Source Edition  4.0]
      Set-Cookie[JSESSIONID=e2121d0c30e627004af713703b3d; Path=/first; HttpOnly]

WebSocket(JSR 356)を使ってみる。その2

先日に続きOracleのサイトをみながらぼちぼち試していきます。

@OnMessageの戻り値

If the return type of the method annotated with @OnMessage is not void, the WebSocket implementation will send the return value to the other peer.

戻り値付のメソッドの場合、その値をpeerに返すというような事が書いてあるので試してみる。

ソース

@OnMessage
public String onMessage(String message) {
System.out.println(“message[” + message + “]”);
 return message;
}

実際にメッセージを送ったブラウザには戻り値が帰ってきたことを確認できた。

戻り値確認

ただし、開いていた他のブラウザには送信されていない。

別ブラウザへの反映状況

 

peerと書いているだけなので複数に返すのでなく単独のものに返すという動作という認識であっているのかな??

疑問は残るけど、複数ブラウザ(peer)に返すサンプルがあるgetAsyncRemotegetBasicRemote()を試してみる。APIによると非同期か同期かの違いらしい。

 @OnMessageのgetAsyncRemote/getBasicRemote

ソースを書き換えてみたけれども、getAsyncRemotegetBasicRemoteの両方とも同時にalert(“Calling”);まで進む。

RemoteEndpoint.Basicをみると下のように書かれているのでのでJSのAlert文で待ちが入るというわけではないらしい。

The point of completion of the send is defined when all the supplied data has been written to the underlying connection

送信失敗に備えたエラー処理をするためのもので、Endpoint間の同期をとる目的でないということでよいのかな。

(java)

static Set<Session> sessions = Collections
.synchronizedSet(new HashSet<Session>());

@OnMessage
public void onMessage(String message) {
System.out.println(“message[” + message + “]”);
for (Session s : sessions) {
s.getAsyncRemote().sendText(message);
}
}

@OnMessage
public void onMessage(String message) throws IOException {
System.out.println(“message[” + message + “]”);
for (Session s : sessions) {
s.getBasicRemote().sendText(message);
}
}

(JS)

socket.onmessage = function(message){
alert(“Calling”);
$(‘#log’).append(message.data + “<br/>”);
}

 

WebSocket(JSR 356)を使ってみる。その1(まずは軽く触れてみる)

EclipseからGlassfishを使う環境は何とかできたっぽいのでWebSocketを使ってみる。

少し前の情報からアノテーションがダイナミックに変わっているので注意。

@WebSocketEndpoint -> @ServerEndpointとかとか(リンク

 

こちらのブログを参考にまずはほぼコピペして動作確認。

キャプチャを簡単にできるかと軽く見てみたけどIE10の開発ツールは特に表示されない模様。

FFのTamperData(プラグイン)では、Connectionがupgradeされていることまでは確認できたけど、upgrade後の動作は見れない模様。コネクションクローズ等も拾えなかった。

WebSocketキャプチャ

(request)

Status=Web Socket Protocol Handshake – 101
X-Powered-By=Servlet/3.1 JSP/2.3 (GlassFish Server Open Source Edition  4.0  Java/Oracle Corporation/1.7)
Server=GlassFish Server Open Source Edition  4.0
Connection=Upgrade
Sec-WebSocket-Accept=ZjJVgnGKxiMQbZjHxqx5bR7qVGY=
Upgrade=websocket

(responce)

Host=localhost:8080
User-Agent=Mozilla/5.0 (Windows NT 6.1; WOW64; rv:22.0) Gecko/20100101 Firefox/22.0
Accept=text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language=ja,en-us;q=0.7,en;q=0.3
Accept-Encoding=gzip, deflate
Sec-WebSocket-Version=13
Origin=http://localhost:8080
Sec-WebSocket-Key=LLDpCnocJ5IY/JIJfax1mw==
Cookie=JSESSIONID=2718b10db58913be398a83618d52
Connection=keep-alive, Upgrade
Pragma=no-cache
Cache-Control=no-cache
Upgrade=websocket

Chromeのデベロッパーツールでも同様に、Upgradeまでしか拾えない模様。

なお、Wiresharkだとキャプチャできるとのことなので後ほど使ってみる。

 

ちなみに下のようにアノテーションを削った場合は404と素直なエラーがでてくれた。

//@ServerEndpoint(“/hello”)
public class HelloWorld {

Status=Not Found – 404
X-Powered-By=Servlet/3.1 JSP/2.3 (GlassFish Server Open Source Edition  4.0  Java/Oracle Corporation/1.7)
Server=GlassFish Server Open Source Edition  4.0