0

In typescript we can send rpc notification like the below code

const webSocket = new WebSocket('ws://www.example.com/socketserver');
rpc.listen({
    webSocket,
    onConnection: (connection: rpc.MessageConnection) => {
        const notification = new rpc.NotificationType<string, void>('testNotification');
        connection.listen();
        connection.sendNotification(notification, 'Hello World');
    }
});

How to do the same thing in Java (Send JONSON-RPC message over websocket)?

I tried the below code in java

           String json = "[{\"jsonrpc\": \"2.0\", \"result\": 19, \"id\": 1}]";
           JsonElement jsonElement = new JsonParser().parse(json);               
           session.getBasicRemote().sendObject(jsonElement);

But I counld able to catch that using the code below(Typescript)

rpc.listen({
    webSocket,
    onConnection: (rpcConnection: rpc.MessageConnection) => {
        const notification = new rpc.NotificationType<string, void>('onInitialize');
        rpcConnection.listen();
        rpcConnection.onNotification(notification,  (param: any) => {
            console.log("awa paams+ "+param); // This prints Hello World
        });

    },  
}); 
Gama11
  • 31,714
  • 9
  • 78
  • 100

0 Answers0