i want create websocket server , send data connected clients, there shall no communication between clients , no data clients server. using webbit https://github.com/webbit/webbit
but unable find method send message clients had create own "solution"
the important code main class
private webserver webserver; private websockethandlerbm handler = new websockethandlerbm(); // ........... private void startserver() { this.webserver = webservers.createwebserver(8080); this.webserver.add("/websocket-echo", this.handler); this.webserver.start(); } // send data this.handler.sendmessage("some data");
class websockethandlerbm:
import java.util.arraylist; import org.webbitserver.basewebsockethandler; import org.webbitserver.websocketconnection; public class websockethandlerbm extends basewebsockethandler { private int connections = 0; private arraylist<websocketconnection> wsconnectionlist = new arraylist<websocketconnection>(); public void deletewsclist() { wsconnectionlist = new arraylist<websocketconnection>(); } private void addwsctolist(websocketconnection wsc) { system.out.println("add 1 " + this.wsconnectionlist.size()); this.wsconnectionlist.add(wsc); system.out.println("add 2 " + this.wsconnectionlist.size()); } private void removewscfromlist(websocketconnection wsc) { system.out.println("rem 1 " + this.wsconnectionlist.size()); this.wsconnectionlist.remove(wsc); system.out.println("rem 2 " + this.wsconnectionlist.size() + "\n##################"); } public void sendmessage(string message) { // avoid problems out of index if list bigger client disconnects while sending arraylist<websocketconnection> tempwsconnectionlist = this.wsconnectionlist; int = 0; while (i < tempwsconnectionlist.size()) { tempwsconnectionlist.get(i).send(message); i++; } } @override public void onopen(websocketconnection connection) { addwsctolist(connection); this.connections++; //connection.send("this webserver saying hello! :)<br/>" + "total no. of subscribers: " + this.connections + "."); } @override public void onclose(websocketconnection connection) { removewscfromlist(connection); this.connections--; } @override public void onmessage(websocketconnection connection, string message) { //connection.send("the server has received following message:<br/>" + message); //sendmessage("fwd: " + message); } }
this not nice works. problem if client did not disconnect correct error
sep 10, 2017 2:29:17 pm org.jboss.netty.channel.defaultchannelpipeline warnung: exception thrown user handler while handling exception event ([edit] exception: java.lang.nullpointerexception) java.util.concurrent.rejectedexecutionexception: task java.util.concurrent.scheduledthreadpoolexecutor$scheduledfuturetask@911b0b rejected java.util.concurrent.scheduledthreadpoolexecutor@7c2947[terminated, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 149909] @ java.util.concurrent.threadpoolexecutor$abortpolicy.rejectedexecution(unknown source) @ java.util.concurrent.threadpoolexecutor.reject(unknown source) @ java.util.concurrent.scheduledthreadpoolexecutor.delayedexecute(unknown source) @ java.util.concurrent.scheduledthreadpoolexecutor.schedule(unknown source) @ java.util.concurrent.scheduledthreadpoolexecutor.execute(unknown source) @ java.util.concurrent.executors$delegatedexecutorservice.execute(unknown source) @ org.webbitserver.netty.connectionhelper.fireconnectionexception(connectionhelper.java:42) @ org.webbitserver.netty.nettyhttpchannelhandler.exceptioncaught(nettyhttpchannelhandler.java:87) @ org.jboss.netty.handler.codec.replay.replayingdecoder.exceptioncaught(replayingdecoder.java:461) @ org.jboss.netty.channel.channels.fireexceptioncaught(channels.java:432) @ org.jboss.netty.channel.abstractchannelsink.exceptioncaught(abstractchannelsink.java:52) @ org.jboss.netty.handler.codec.oneone.onetooneencoder.handledownstream(onetooneencoder.java:68) @ org.jboss.netty.handler.codec.http.httpcontentencoder.writerequested(httpcontentencoder.java:167) @ org.jboss.netty.channel.channels.write(channels.java:611) @ org.jboss.netty.channel.channels.write(channels.java:578) @ org.jboss.netty.channel.abstractchannel.write(abstractchannel.java:251) @ org.webbitserver.netty.abstracthttpconnection.writemessage(abstracthttpconnection.java:25) @ org.webbitserver.netty.nettywebsocketconnection.send(nettywebsocketconnection.java:29) @ org.webbitserver.netty.nettywebsocketconnection.send(nettywebsocketconnection.java:13) @ de.spacesupport.bmmqtt.websockethandlerbm.sendmessage(websockethandlerbm.java:32) @ de.spacesupport.bmmqtt.bmmqtt.handlemqttanswer(bmmqtt.java:914) @ de.spacesupport.bmmqtt.bmmqtt.access$12(bmmqtt.java:543) @ de.spacesupport.bmmqtt.bmmqtt$12.messagearrived(bmmqtt.java:972) @ org.eclipse.paho.client.mqttv3.internal.commscallback.delivermessage(commscallback.java:475) @ org.eclipse.paho.client.mqttv3.internal.commscallback.handlemessage(commscallback.java:379) @ org.eclipse.paho.client.mqttv3.internal.commscallback.run(commscallback.java:183) @ java.lang.thread.run(unknown source)
the trigger line
tempwsconnectionlist.get(i).send(message);
and error no references code
sep 10, 2017 7:20:56 pm org.jboss.netty.channel.defaultchannelpipeline warnung: exception thrown user handler while handling exception event ([edit] exception: java.lang.illegalstateexception: cannot send more responses requests) java.util.concurrent.rejectedexecutionexception: task java.util.concurrent.scheduledthreadpoolexecutor$scheduledfuturetask@c3954d rejected java.util.concurrent.scheduledthreadpoolexecutor@822a30[shutting down, pool size = 1, active threads = 1, queued tasks = 71478, completed tasks = 4037486] @ java.util.concurrent.threadpoolexecutor$abortpolicy.rejectedexecution(unknown source) @ java.util.concurrent.threadpoolexecutor.reject(unknown source) @ java.util.concurrent.scheduledthreadpoolexecutor.delayedexecute(unknown source) @ java.util.concurrent.scheduledthreadpoolexecutor.schedule(unknown source) @ java.util.concurrent.scheduledthreadpoolexecutor.execute(unknown source) @ java.util.concurrent.executors$delegatedexecutorservice.execute(unknown source) @ org.webbitserver.netty.connectionhelper.fireconnectionexception(connectionhelper.java:42) @ org.webbitserver.netty.nettyhttpchannelhandler.exceptioncaught(nettyhttpchannelhandler.java:87) @ org.jboss.netty.handler.codec.replay.replayingdecoder.exceptioncaught(replayingdecoder.java:461) @ org.jboss.netty.channel.channels.fireexceptioncaught(channels.java:432) @ org.jboss.netty.channel.abstractchannelsink.exceptioncaught(abstractchannelsink.java:52) @ org.jboss.netty.channel.channels.write(channels.java:611) @ org.jboss.netty.channel.channels.write(channels.java:578) @ org.jboss.netty.channel.abstractchannel.write(abstractchannel.java:251) @ org.webbitserver.netty.nettyhttpresponse.write(nettyhttpresponse.java:208) @ org.webbitserver.netty.nettyhttpresponse.flushresponse(nettyhttpresponse.java:196) @ org.webbitserver.netty.nettyhttpresponse.error(nettyhttpresponse.java:169) @ org.webbitserver.netty.nettyhttpchannelhandler$3.run(nettyhttpchannelhandler.java:94) @ java.util.concurrent.executors$runnableadapter.call(unknown source) @ java.util.concurrent.futuretask.run(unknown source) @ java.util.concurrent.scheduledthreadpoolexecutor$scheduledfuturetask.access$201(unknown source) @ java.util.concurrent.scheduledthreadpoolexecutor$scheduledfuturetask.run(unknown source) @ java.util.concurrent.threadpoolexecutor.runworker(unknown source) @ java.util.concurrent.threadpoolexecutor$worker.run(unknown source) @ java.lang.thread.run(unknown source) org.webbitserver.webbitexception: cannot send more responses requests on [edit] @ org.webbitserver.webbitexception.fromexception(webbitexception.java:36) @ org.webbitserver.netty.nettyhttpresponse.error(nettyhttpresponse.java:171) @ org.webbitserver.netty.nettyhttpchannelhandler$3.run(nettyhttpchannelhandler.java:94) @ java.util.concurrent.executors$runnableadapter.call(unknown source) @ java.util.concurrent.futuretask.run(unknown source) @ java.util.concurrent.scheduledthreadpoolexecutor$scheduledfuturetask.access$201(unknown source) @ java.util.concurrent.scheduledthreadpoolexecutor$scheduledfuturetask.run(unknown source) @ java.util.concurrent.threadpoolexecutor.runworker(unknown source) @ java.util.concurrent.threadpoolexecutor$worker.run(unknown source) @ java.lang.thread.run(unknown source) caused by: java.lang.illegalstateexception: cannot send more responses requests @ org.jboss.netty.handler.codec.http.httpcontentencoder.writerequested(httpcontentencoder.java:108) @ org.jboss.netty.channel.channels.write(channels.java:611) @ org.jboss.netty.channel.channels.write(channels.java:578) @ org.jboss.netty.channel.abstractchannel.write(abstractchannel.java:251) @ org.webbitserver.netty.nettyhttpresponse.write(nettyhttpresponse.java:208) @ org.webbitserver.netty.nettyhttpresponse.flushresponse(nettyhttpresponse.java:196) @ org.webbitserver.netty.nettyhttpresponse.error(nettyhttpresponse.java:169) ... 8 more
any ideas ?
Comments
Post a Comment