raksheetbhat
raksheetbhat

Reputation: 960

How to initiate server side message in WebSockets Java?

I'm using the javax.websocket API in Java. I am using a Jetty server and Javascript for the client. How do I initiate sendMessage from the server?

Details: Am using a jetty-maven-plugin 9.4.8.v20171121.

Server-side dependencies: org.eclipse.jetty.websocket - websocket-server and javax-websocket-server-impl.

Here's my server code:

package com.trice.server.web;

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

@ServerEndpoint("/WebSocketTestServlet")
public class WebSocketTestServlet {

    @OnOpen
    public void onOpen(){
        System.out.println("Open Connection ...");
    }

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

    @OnMessage
    public String onMessage(String message){
        System.out.println("Message from the client: " + message);
        String echoMsg = "Echo from the server : " + message;
        return echoMsg;
    }

    @OnError
    public void onError(Throwable e){
        e.printStackTrace();
    }
}

And client code:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Tomcat WebSocket</title>
</head>
<body>
    <form>
        <input id="message" type="text">
        <input onclick="wsSendMessage();" value="Echo" type="button">
        <input onclick="wsCloseConnection();" value="Disconnect" type="button">
    </form>
    <br>
    <textarea id="echoText" rows="5" cols="30"></textarea>
    <script type="text/javascript">
        var webSocket = new WebSocket("ws://localhost:8080/WebSocketTestServlet");
        var echoText = document.getElementById("echoText");
        echoText.value = "";
        var message = document.getElementById("message");
        webSocket.onopen = function(message){ wsOpen(message);};
        webSocket.onmessage = function(message){ wsGetMessage(message);};
        webSocket.onclose = function(message){ wsClose(message);};
        webSocket.onerror = function(message){ wsError(message);};
        function wsOpen(message){
            echoText.value += "Connected ... \n";
        }
        function wsSendMessage(){
            webSocket.send(message.value);
            echoText.value += "Message sent to the server : " + message.value + "\n";
            message.value = "";
        }
        function wsCloseConnection(){
            webSocket.close();
        }
        function wsGetMessage(message){
            echoText.value += "Message received from to the server : " + message.data + "\n";
        }
        function wsClose(message){
            echoText.value += "Disconnect ... \n";
            console.log("disconnect", message);
        }

        function wsError(message){
            echoText.value += "Error \n";
            console.log("error", message);
        }
    </script>
</body>
</html>

Reference link

Appreciate any help. Thanks.

Upvotes: 0

Views: 1592

Answers (1)

raksheetbhat
raksheetbhat

Reputation: 960

There was a very easy answer to my question. All I needed to do was:

session.getBasicRemote().sendText("Some string");

Upvotes: 1

Related Questions