Java Code Examples for javax.websocket.RemoteEndpoint.Basic#sendText()

The following examples show how to use javax.websocket.RemoteEndpoint.Basic#sendText() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractWsController.java    From springboot-learn with MIT License 6 votes vote down vote up
/**
 * 同步方式向客户端发送字符串
 *
 * @param msg 参数类型为String或ByteBuffer
 */
protected <T> void call(T msg) {
	try {
		synchronized (this) {
			Basic remote = this.getSession().getBasicRemote();
			if (msg instanceof String) {
				remote.sendText((String) msg);
			} else if (msg instanceof ByteBuffer) {
				remote.sendBinary((ByteBuffer) msg);
			}

		}
	} catch (IOException e) {
		try {
			this.getSession().close();
		} catch (IOException ignored) {
		}
		onClose();
	}
}
 
Example 2
Source File: TesterFirehoseServer.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@OnMessage
public void onMessage(Session session, String msg) throws IOException {

    if (started) {
        return;
    }
    synchronized (this) {
        if (started) {
            return;
        } else {
            started = true;
        }
    }

    System.out.println("Received " + msg + ", now sending data");

    session.getUserProperties().put(
            org.apache.tomcat.websocket.Constants.BLOCKING_SEND_TIMEOUT_PROPERTY,
            Long.valueOf(SEND_TIME_OUT_MILLIS));

    Basic remote = session.getBasicRemote();
    remote.setBatchingAllowed(true);

    for (int i = 0; i < MESSAGE_COUNT; i++) {
        remote.sendText(MESSAGE);
        if (i % (MESSAGE_COUNT * 0.4) == 0) {
            remote.setBatchingAllowed(false);
            remote.setBatchingAllowed(true);
        }
    }

    // Flushing should happen automatically on session close
    session.close();
}
 
Example 3
Source File: TesterFirehoseServer.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
@OnMessage
public void onMessage(Session session, String msg) throws IOException {

    if (started) {
        return;
    }
    synchronized (this) {
        if (started) {
            return;
        } else {
            started = true;
        }
    }

    System.out.println("Received " + msg + ", now sending data");

    session.getUserProperties().put(
            "org.apache.tomcat.websocket.BLOCKING_SEND_TIMEOUT",
            Long.valueOf(SEND_TIME_OUT_MILLIS));

    Basic remote = session.getBasicRemote();
    remote.setBatchingAllowed(true);

    for (int i = 0; i < MESSAGE_COUNT; i++) {
        remote.sendText(MESSAGE);
        if (i % (MESSAGE_COUNT * 0.4) == 0) {
            remote.setBatchingAllowed(false);
            remote.setBatchingAllowed(true);
        }
    }

    // Flushing should happen automatically on session close
    session.close();
}
 
Example 4
Source File: TesterFirehoseServer.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
@OnMessage
public void onMessage(Session session, String msg) throws IOException {

    if (started) {
        return;
    }
    synchronized (this) {
        if (started) {
            return;
        } else {
            started = true;
        }
    }

    System.out.println("Received " + msg + ", now sending data");

    session.getUserProperties().put(
            "org.apache.tomcat.websocket.BLOCKING_SEND_TIMEOUT",
            Long.valueOf(SEND_TIME_OUT_MILLIS));

    Basic remote = session.getBasicRemote();
    remote.setBatchingAllowed(true);

    for (int i = 0; i < MESSAGE_COUNT; i++) {
        remote.sendText(MESSAGE);
        if (i % (MESSAGE_COUNT * 0.4) == 0) {
            remote.setBatchingAllowed(false);
            remote.setBatchingAllowed(true);
        }
    }

    // Flushing should happen automatically on session close
    session.close();
}
 
Example 5
Source File: LibertyClientBrowserSessionWrapper.java    From rogue-cloud with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unused")
		private void innerRun() throws IOException, InterruptedException {
			Basic b;
			synchronized(session_synch) {
				b = session_synch.getBasicRemote();
			}
			
			List<String> localMessagesToSend = new ArrayList<>();
			
			while(isSessionOpenUnsynchronized() && !isInterrupted()) {

				if(SIMULATE_INTERMITTENT_FAILURES && Math.random() < 0.1) {
					try {
						session_synch.close(); 
						System.out.println("nuking browser.");
						
					} catch(Exception e) {
						/* ignore */
					}
				}
				
				synchronized (messagesToSend_synch) {
					if(messagesToSend_synch.size() == 0) {
						messagesToSend_synch.wait(1000);
					}
					
					localMessagesToSend.addAll(messagesToSend_synch);
					messagesToSend_synch.clear();
					
				}
				
				synchronized(session_synch) {
					for(String str : localMessagesToSend) {
						b.sendText(str);
//						System.out.println("sending text: "+str);
					}
				}
				
				localMessagesToSend.clear();
			}
		}
 
Example 6
Source File: ActiveWSClientSession.java    From rogue-cloud with Apache License 2.0 4 votes vote down vote up
@Override
public void run() {

	log.info("ActiveWSClientSessionSender started for "+session_synch, logContext);
	
	try {
		boolean continueLoop = true;
		
		Basic b;
		synchronized(session_synch) {
			b = session_synch.getBasicRemote();
		}
	
		List<String> localStringsToSend = new ArrayList<String>();
	
		while(continueLoop) {
			
			if(roundScope.isRoundComplete()) {
				log.interesting("Exiting loop on round expiration.", logContext);
				continueLoop = false;
			}
			
			synchronized(stringsToSend_synch) {
				
				stringsToSend_synch.wait(10000); // TODO: CURR - Uhh?
				
				localStringsToSend.addAll(stringsToSend_synch);
				stringsToSend_synch.clear();
				
			}
	
			synchronized(session_synch) {
				
				if(session_synch.isOpen()) {

					for(String str : localStringsToSend) {
						if(NG.ENABLED) { NG.log(RCRuntime.GAME_TICKS.get(), "Writing to client in AWSClientSession: "+str); }
						log.info("Writing out ["+type.name()+" ("+str.getBytes().length+")]: "+str, logContext);
						if(type == Type.BROWSER) {
							b.sendText(str);
						} else {
							b.sendBinary(CompressionUtils.compressToByteBuffer(str));	
						}
						
					}
					
					localStringsToSend.clear();
					
				} else {
					continueLoop = false;
				}
				
			}
			
		}
	} catch(InterruptedException ie) {
		/* ignore*/
	} catch(Throwable t) {
		log.severe("Error occured on session sender", t, logContext);
		t.printStackTrace();
	
	} finally {
		log.info("ActiveWSClientSessionSender ended for "+session_synch, logContext);
		
		ServerUtil.runInAnonymousThread( () -> {
			try {
				session_synch.close(); // intentional non-synch
			} catch (IOException e) {
				/* ignore*/
			}
		});

	}
	
}