Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A sample WebSocket client application using nv-websocket-client library.
/*
* Copyright (C) 2015 Neo Visionaries Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific
* language governing permissions and limitations under the
* License.
*/
import java.io.*;
import com.neovisionaries.ws.client.*;
/**
* A sample WebSocket client application using nv-websocket-client
* library.
*
* <p>
* This application connects to the echo server on websocket.org
* ({@code ws://echo.websocket.org}) and repeats to (1) read a
* line from the standard input, (2) send the read line to the
* server and (3) print the response from the server, until
* {@code exit} is entered.
* </p>
*
* @see <a href="https://github.com/TakahikoKawasaki/nv-websocket-client"
* >nv-websocket-client</a>
*
* @author Takahiko Kawasaki
*/
public class EchoClient
{
/**
* The echo server on websocket.org.
*/
private static final String SERVER = "ws://echo.websocket.org";
/**
* The timeout value in milliseconds for socket connection.
*/
private static final int TIMEOUT = 5000;
/**
* The entry point of this command line application.
*/
public static void main(String[] args) throws Exception
{
// Connect to the echo server.
WebSocket ws = connect();
// The standard input via BufferedReader.
BufferedReader in = getInput();
// A text read from the standard input.
String text;
// Read lines until "exit" is entered.
while ((text = in.readLine()) != null)
{
// If the input string is "exit".
if (text.equals("exit"))
{
// Finish this application.
break;
}
// Send the text to the server.
ws.sendText(text);
}
// Close the web socket.
ws.disconnect();
}
/**
* Connect to the server.
*/
private static WebSocket connect() throws Exception
{
return new WebSocketFactory()
.setConnectionTimeout(TIMEOUT)
.createSocket(SERVER)
.addListener(new WebSocketAdapter() {
// A text message arrived from the server.
public void onTextMessage(WebSocket websocket, String message) {
System.out.println(message);
}
})
.addExtension(WebSocketExtension.PERMESSAGE_DEFLATE)
.connect();
}
/**
* Wrap the standard input with BufferedReader.
*/
private static BufferedReader getInput() throws IOException
{
return new BufferedReader(new InputStreamReader(System.in));
}
}
@alcnsahin

This comment has been minimized.

Copy link

@alcnsahin alcnsahin commented Dec 27, 2018

Hello,

This sample does work well. But I need to post some specific data to rest api while data comes from websocket server. How can handle it?

Thanks.

@ahmedcooll

This comment has been minimized.

Copy link

@ahmedcooll ahmedcooll commented Aug 14, 2020

Hi,
return new BufferedReader(new InputStreamReader(System.in));

what will be the input for this - sample try. Please guide me.

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.