Last active
August 14, 2020 09:39
-
-
Save TakahikoKawasaki/e79d36bf91bf9508ddd2 to your computer and use it in GitHub Desktop.
A sample WebSocket client application using nv-websocket-client library.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi,
return new BufferedReader(new InputStreamReader(System.in));
what will be the input for this - sample try. Please guide me.
Thanks