I run simple websocket server on Tomcat:
package server.ws;
import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.server.ServerEndpoint;
#ServerEndpoint("/websocketendpoint")
public class WsServer {
#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();
}
}
Client that connects to my server complains regarding Tomcat closes session after echo was transmitted. How to make the server to not close the connection after echo?
i'm not sure, but i guess onMessagehas no return value.
for me, the following code works fine and i have any problems
#OnMessage
public void message(final String message, final javax.websocket.Session session) throws IOException {
for (final Session s : session.getOpenSessions()) {
final Basic endpoint = s.getBasicRemote();
endpoint.sendText(message);
}
}
Related
WS Server code:
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.Local;
import javax.websocket.*;
import javax.websocket.server.ServerEndpoint;
#ServerEndpoint(value = "/websockets/broadcast-server")
#Local
public class BroadcastServer {
private static final Logger LOGGER = Logger.getLogger(BroadcastServer.class.getName());
private final Set<Session> sessions = new HashSet<>();
#OnMessage
public String onMessage(String message, Session session) throws IOException {
LOGGER.log(Level.INFO, "Received message: {0}", message);
broadcastMessage("!!!BROADCAST!!! " + message);
return "Echo: " + message;
}
#OnOpen
public void onOpen(Session session) throws IOException {
session.getBasicRemote().sendText("* The session is open.");
LOGGER.info("The session is open.");
this.sessions.add(session);
}
#OnClose
public void onClose(Session session) throws IOException {
LOGGER.info("The session is close.");
this.sessions.remove(session);
}
#OnError
public void onError(Session session, Throwable error) throws Exception {
session.getBasicRemote().sendText("* Error happen. " + error.getMessage());
LOGGER.warning("Error happen. " + error.getMessage());
}
public void broadcastMessage(String message) throws IOException {
LOGGER.info("Number of sessions: " + this.sessions.size());
for (Session session : this.sessions) {
session.getBasicRemote().sendText(message);
}
}
}
Unit test/WS client code:
public class BroadcastClientTest {
#Test
public void test() throws Exception {
BroadcastClient client = new BroadcastClient();
client.connect(URI.create("ws://localhost:7001/ee-ejb/websockets/broadcast-server"));
client.send("Hello World!");
Thread.sleep(Integer.MAX_VALUE);
}
}
If I run a few instances of above Unit test then each client sends a single message to the server and receive a single response.
But I expect to receive more messages because every message should be broadcasted to all clients.
It feels like for each client WebLogic creates separate WS server instance.
How to broadcast a message?
Broadcast in WebLogic was successfully done with following code:
#OnMessage
public void onMessage(String message, Session session) throws IOException {
for (Session sess : session.getOpenSessions()) {
sess.getBasicRemote().sendText("Echo: " + message);
}
}
I have websocket server and now i need client to test its usage. I am using this clients code:
import org.msgpack.MessagePack;
import org.springframework.web.socket.BinaryMessage;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
public class TestApp {
public static void main(String[] args) {
try {
// open websocket
final WebsocketClientEndpoint clientEndPoint = new WebsocketClientEndpoint(new URI("ws://localhost:8080/websocket"));
// add listener
clientEndPoint.addMessageHandler(new WebsocketClientEndpoint.MessageHandler() {
public void handleMessage(String message) {
System.out.println(message);
}
});
// send message to websocket
clientEndPoint.sendMessage(new BinaryMessage(...));
// wait 5 seconds for messages from websocket
Thread.sleep(5000);
} catch (InterruptedException ex) {
System.err.println("InterruptedException exception: " + ex.getMessage());
} catch (URISyntaxException ex) {
System.err.println("URISyntaxException exception: " + ex.getMessage());
} catch (IOException e) {
e.printStackTrace();
}
}
}
import org.springframework.web.socket.BinaryMessage;
import java.net.URI;
import javax.websocket.ClientEndpoint;
import javax.websocket.CloseReason;
import javax.websocket.ContainerProvider;
import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.WebSocketContainer;
#ClientEndpoint
class WebsocketClientEndpoint {
Session userSession = null;
private MessageHandler messageHandler;
public WebsocketClientEndpoint(URI endpointURI) {
try {
WebSocketContainer container = ContainerProvider.getWebSocketContainer();
container.connectToServer(this, endpointURI);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* Callback hook for Connection open events.
*
* #param userSession the userSession which is opened.
*/
#OnOpen
public void onOpen(Session userSession) {
System.out.println("opening websocket");
this.userSession = userSession;
}
/**
* Callback hook for Connection close events.
*
* #param userSession the userSession which is getting closed.
* #param reason the reason for connection close
*/
#OnClose
public void onClose(Session userSession, CloseReason reason) {
System.out.println("closing websocket");
this.userSession = null;
}
/**
* Callback hook for Message Events. This method will be invoked when a client send a message.
*
* #param message The text message
*/
#OnMessage
public void onMessage(String message) {
if (this.messageHandler != null) {
this.messageHandler.handleMessage(message);
}
}
/**
* register message handler
*
* #param msgHandler
*/
public void addMessageHandler(MessageHandler msgHandler) {
this.messageHandler = msgHandler;
}
/**
* Send a message.
*
* #param message
*/
public void sendMessage(BinaryMessage message) {
this.userSession.getAsyncRemote().sendObject(message);
}
/**
* Message handler.
*
* #author Jiji_Sasidharan
*/
public static interface MessageHandler {
public void handleMessage(String message);
}
}
When i run this client, my sever accepts it, i am using spring and
void afterConnectionEstablished(WebSocketSession session)
method of BinaryWebSocketHandler fires up and everything seems fine, however after this method, the client does not set userSession , it is still null so it always throw nullpointer exception on
this.userSession.getAsyncRemote().sendObject(message);
which is weird cuz server accepts connection. What is causing this problem? Is there a fix for it?
Thanks for help!
package websocket.client;
import java.net.URI;
import javax.websocket.*;
#ClientEndpoint
public class WSClient {
private static Object waitLock = new Object();
#OnMessage
public void onMessage(String message) {
//the new USD rate arrives from the websocket server side.
System.out.println("Received msg: " + message);
}
private static void wait4TerminateSignal() {
synchronized(waitLock) {
try {
waitLock.wait();
} catch (InterruptedException e) {}
}
}
public static void main(String[] args) {
WebSocketContainer container = null; //
Session session = null;
try {
//Tyrus is plugged via ServiceLoader API. See notes above
container = ContainerProvider.getWebSocketContainer();
//WS1 is the context-root of my web.app
//ratesrv is the path given in the ServerEndPoint annotation on server implementation
session = container.connectToServer(WSClient.class, URI.create("ws://localhost:8080/WS1/ratesrv"));
wait4TerminateSignal();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (session != null) {
try {
session.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
I have simple smack 3.2.1 application. It connect to xmpp server
and waiting for another user conversation initiation. When user ask question
via chat, application send response (answer). And it is working fine. Here is code:
JabberApplication.java:
package jabberapplication;
import org.jivesoftware.smack.XMPPException;
public class JabberApplication {
public static void main(String[] args) throws XMPPException, InterruptedException {
String username = "USERNAME";
String password = "PASSWORD";
String server = "SERVER";
int port=5222;
XmppManager xmppManager = new XmppManager(server, port);
xmppManager.init();
xmppManager.performLogin(username, password);
xmppManager.setStatus(true, "Hello everyone");
boolean isRunning = true;
while (isRunning) {
Thread.sleep(50);
}
xmppManager.destroy();
}
}
XmppManager.java:
package jabberapplication;
import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.ChatManager;
import org.jivesoftware.smack.ChatManagerListener;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.packet.Presence.Type;
public class XmppManager {
private final String server;
private final int port;
private XMPPConnection connection;
private ChatManager chatManager;
private MessageListener messageListener;
private ConversationController conversationController;
public XmppManager(String server, int port) {
this.server = server;
this.port = port;
}
public void init() throws XMPPException {
System.out.println(String.format("Initializing connection to server %1$s port %2$d", server, port));
ConnectionConfiguration connectionConfiguration = new ConnectionConfiguration(server, port);
connection = new XMPPConnection(connectionConfiguration);
connection.connect();
System.out.println("Connected: " + connection.isConnected());
chatManager = connection.getChatManager();
chatManager.addChatListener(new MyChatManagerListener());
messageListener = new MyMessageListener();
conversationController = new ConversationController();
}
public void performLogin(String username, String password) throws XMPPException {
if (connection != null && connection.isConnected()) {
connection.login(username, password);
}
}
public void setStatus(boolean available, String status) {
Presence.Type type = available ? Type.available : Type.unavailable;
Presence presence = new Presence(type);
presence.setStatus(status);
connection.sendPacket(presence);
}
public void destroy() {
if (connection != null && connection.isConnected()) {
connection.disconnect();
}
}
public void sendMessage(String message, String buddyJID) throws XMPPException {
System.out.println(String.format("Sending mesage '%1$s' to user %2$s", message, buddyJID));
Chat chat = chatManager.createChat(buddyJID, messageListener);
chat.sendMessage(message);
}
class MyMessageListener implements MessageListener {
#Override
public void processMessage(Chat chat, Message message) {
String from = message.getFrom();
String body = message.getBody();
if (!body.equals("null")) {
System.out.println(String.format("Received message '%1$s' from %2$s", body, from));
try {
chat.sendMessage(conversationController.getAnswer(body));
} catch (XMPPException ex) {
System.out.println(ex.getMessage());
}
}
}
}
class MyChatManagerListener implements ChatManagerListener {
#Override
public void chatCreated(Chat chat, boolean bln) {
int indexAt = chat.getParticipant().indexOf("#");
String username = chat.getParticipant().substring(0, indexAt);
chat.addMessageListener(messageListener);
try {
chat.sendMessage("Hello " + username + " !");
} catch (XMPPException ex) {
System.out.println(ex.getMessage());
}
}
}
}
Well, the question is: How to write similar app using smack library version 4.2.3. It looks like that in 4.2.3 there are no MessageListener and ChatManagerListener classes. Any suggestions ?
Best Regards.
For Smack 4.2.3, ChatManagerListener interface is under org.jivesoftware.smack.chat class (note : chat not Chat).
So, you need to change import to org.jivesoftware.smack.chat.ChatManagerListener.
Also, MessageListener is now change to ChatMessageListener also under org.jivesoftware.smack.chat class.
So, you need to change import to org.jivesoftware.smack.chat.ChatMessageListener.
And rename the implements to as below :
class MyMessageListener implements ChatMessageListener
I am new in web socket. I had learned some tutorial to implement web socket. But it is not working properly. I don't know what I have done wrongly.
note: I have not done any configuration (web.xml) and I have been used tomcat 7.0.47,jdk 1.6.0_24
This is My Server side code:
package com.hmkcode;
import java.io.IOException;
import java.util.LinkedList;
import javax.websocket.EncodeException;
import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
#ServerEndpoint(value="/websocket/{client-id}")
public class MyServerEndpoint {
private static final LinkedList<Session> clients = new LinkedList<Session>();
#OnOpen
public void onOpen(Session session) {
clients.add(session);
}
#OnMessage
public void onMessage(String message,#PathParam("client-id") String clientId) {
for (Session client : clients) {
try {
client.getBasicRemote().sendObject(clientId+": "+message);
} catch (IOException e) {
e.printStackTrace();
} catch (EncodeException e) {
e.printStackTrace();
}
}
}
#OnClose
public void onClose(Session peer) {
clients.remove(peer);
}
}
This is my Web client code
var URL = "ws://localhost:8080/WebSocket1/websocket/web-client";
var websocket;
$(document).ready(function(){
connect();
});
function connect(){
websocket = new WebSocket(URL);
websocket.onopen = function(evnt) { onOpen(evnt);};
websocket.onmessage = function(evnt) { onMessage(evnt); };
websocket.onerror = function(evnt) { onError(evnt); };
}
function sendMessage() {
websocket.send($("#message").val());
}
function onOpen() {
updateStatus("connected");
}
function onMessage(evnt) {
if (typeof evnt.data == "string") {
$("#received_messages").append(
$('<tr/>')
.append($('<td/>').text("1"))
.append($('<td/>').text(evnt.data.substring(0,evnt.data.indexOf(":"))))
.append($('<td/>').text(evnt.data.substring(evnt.data.indexOf(":")+1))));
}
}
function onError(evnt) {
alert('ERROR: ' + evnt.data);
}
function updateStatus(status){
if(status == "connected"){
$("#status").removeClass (function (index, css) {
return (css.match (/\blabel-\S+/g) || []).join(' ');
});
$("#status").text(status).addClass("label-success");
}
}
My guess is...
Since you ServerEndpoint maps an argument "{client-id}" it should be present in the URL your client uses to connect.
Either you remove the parameter on server-side or you need to include a value to it on client-side.
Again, just guessing. It would be useful if you include more information regarding the issue (e.g., server logs)
[]s
Heleno
hi i am using this codes for rmi
RmiServer.java
import java.rmi.*;
import java.rmi.registry.*;
import java.rmi.server.*;
import java.net.*;
public class RmiServer extends java.rmi.server.UnicastRemoteObject
implements ReceiveMessageInterface
{
int thisPort;
String thisAddress;
Registry registry; // rmi registry for lookup the remote objects.
// This method is called from the remote client by the RMI.
// This is the implementation of the gReceiveMessageInterfaceh.
public void receiveMessage(String x) throws RemoteException
{
System.out.println(x);
}
public RmiServer() throws RemoteException
{
try{
// get the address of this host.
thisAddress= (InetAddress.getLocalHost()).toString();
}
catch(Exception e){
throw new RemoteException("can't get inet address.");
}
thisPort=3232; // this port(registryfs port)
System.out.println("this address="+thisAddress+",port="+thisPort);
try{
// create the registry and bind the name and object.
registry = LocateRegistry.createRegistry( thisPort );
registry.rebind("rmiServer", this);
}
catch(RemoteException e){
throw e;
}
}
static public void main(String args[])
{
try{
RmiServer s=new RmiServer();
}
catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
}
RmiClient.java
import java.rmi.*;
import java.rmi.registry.*;
import java.net.*;
public class RmiClient
{
static public void main(String args[])
{
ReceiveMessageInterface rmiServer;
Registry registry;
String serverAddress=args[0];
String serverPort=args[1];
String text=args[2];
System.out.println("sending "+text+" to "+serverAddress+":"+serverPort);
try{
// get the �gregistry�h
registry=LocateRegistry.getRegistry(
serverAddress,
(new Integer(serverPort)).intValue()
);
// look up the remote object
rmiServer=
(ReceiveMessageInterface)(registry.lookup("rmiServer"));
// call the remote method
rmiServer.receiveMessage(text);
}
catch(RemoteException e){
e.printStackTrace();
}
catch(NotBoundException e){
e.printStackTrace();
}
}
}
ReceiveMessageInterface.java
import java.rmi.*;
public interface ReceiveMessageInterface extends Remote
{
public void receiveMessage(String x) throws RemoteException;
}
This works fine normally , but when the a computer is connected to internet through mobile or it shares internet from other pc it doesn't work
I get this error.
java.net.connectexception connection timeout
when i tried to telnet it fails to connect but when i try to run this program that pc
to my pc it works.
Please let me know how to solve this issue.
Sounds like a firewall or proxy server issue.