本文整理汇总了Java中org.springframework.web.socket.TextMessage类的典型用法代码示例。如果您正苦于以下问题:Java TextMessage类的具体用法?Java TextMessage怎么用?Java TextMessage使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
TextMessage类属于org.springframework.web.socket包,在下文中一共展示了TextMessage类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: sendPlayEnd
import org.springframework.web.socket.TextMessage; //导入依赖的package包/类
public void sendPlayEnd(WebSocketSession session) {
try {
JsonObject response = new JsonObject();
response.addProperty("id", "playEnd");
session.sendMessage(new TextMessage(response.toString()));
} catch (IOException e) {
log.error("Error sending playEndOfStream message", e);
}
// Release pipeline
pipeline.release();
this.webRtc = null;
}
示例2: handleTextMessage
import org.springframework.web.socket.TextMessage; //导入依赖的package包/类
protected void handleTextMessage(WebSocketSession session, TextMessage textMessage) throws Exception
{
WebSocketMessage message = objectMapper.readerFor(WebSocketMessage.class).readValue(textMessage.getPayload());
log.info("Got message {} from session {}.", message, session.getId());
if (message instanceof SubscribeRequest)
{
SubscribeRequest subscribeRequest = (SubscribeRequest) message;
this.subscribeBusinessService.addSubscription(session, subscribeRequest.getBucketName());
}
else if (message instanceof PublishRequest)
{
PublishRequest publishRequest = (PublishRequest) message;
this.publishBucketBusinessService.publish(new BucketDto(publishRequest.getBucketName(), publishRequest.getData()));
}
else
{
log.debug("Unhandled message {} ignored.", message);
}
}
示例3: sendWebSocketMessage
import org.springframework.web.socket.TextMessage; //导入依赖的package包/类
public void sendWebSocketMessage(ProgressVo msg) {
if (webSocketSession == null || !webSocketSession.isOpen()) {
return;
}
int curPercent = 0;
if (msg.getMax() != 0) {
curPercent = (int) Math.floor(Double.valueOf(msg.getProgress()) / msg.getMax() * 100);
}
if (lastPercentProcess != curPercent) {
lastPercentProcess = curPercent;
Gson gson = new Gson();
String jsonStr = gson.toJson(msg);
TextMessage message = new TextMessage(jsonStr);
try {
webSocketSession.sendMessage(message);
} catch (Exception e) {
try {
webSocketSession.close();
} catch (IOException e1) {
e1.printStackTrace();
}
e.printStackTrace();
}
}
}
示例4: handleTextMessage
import org.springframework.web.socket.TextMessage; //导入依赖的package包/类
@Override
protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
super.handleTextMessage(session, message);
try {
getClient(session,null);
if (this.terminalClient != null ) {
if ( !terminalClient.isClosed()) {
terminalClient.write(message.getPayload());
}else {
session.close();
}
}
} catch (Exception e) {
session.sendMessage(new TextMessage("Sorry! opencron Terminal was closed, please try again. "));
terminalClient.disconnect();
session.close();
}
}
示例5: afterConnectionEstablished
import org.springframework.web.socket.TextMessage; //导入依赖的package包/类
@Override
public synchronized void afterConnectionEstablished(final WebSocketSession session) throws Exception {
super.afterConnectionEstablished(session);
session.sendMessage(new TextMessage("\nWeb-socket timer is on!\n"));
new Thread() {
@Override
public synchronized void run() {
try {
while (session.isOpen()) {
session.sendMessage(new TextMessage("" + new Date() + "\n"));
Thread.currentThread().wait(1000);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
.start();
}
示例6: sendMessage
import org.springframework.web.socket.TextMessage; //导入依赖的package包/类
public void sendMessage(WampMessage msg) throws IOException {
try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
JsonGenerator generator = this.jsonFactory.createGenerator(bos)) {
generator.writeStartArray();
msg.serialize(generator);
generator.writeEndArray();
generator.close();
if (this.isBinary) {
this.webSocketSession.sendMessage(new BinaryMessage(bos.toByteArray()));
}
else {
this.webSocketSession.sendMessage(new TextMessage(bos.toByteArray()));
}
}
}
示例7: handleTextMessage
import org.springframework.web.socket.TextMessage; //导入依赖的package包/类
@Override
protected void handleTextMessage(WebSocketSession session, TextMessage message)
throws Exception {
try {
WampMessage wampMessage = WampMessage.deserialize(this.jsonFactory,
message.asBytes());
if (wampMessage instanceof WelcomeMessage) {
this.welcomeMessageFuture.complete((WelcomeMessage) wampMessage);
}
else {
this.receivedMessages.add(wampMessage);
if (this.receivedMessages.size() == this.noOfResults) {
this.messageFuture.complete(this.receivedMessages);
}
}
}
catch (IOException e) {
this.welcomeMessageFuture.completeExceptionally(e);
this.messageFuture.completeExceptionally(e);
}
}
示例8: broadcast
import org.springframework.web.socket.TextMessage; //导入依赖的package包/类
/**
* 广播给用户信息 可以除开某个id
*
* @param msg 信息
* @param except 除开的id
*/
public void broadcast(TextMessage msg, String except) {
threadPool.execute(() -> {
for (Map.Entry<String, WebSocketSession> entry : clients.entrySet()) {
WebSocketSession se = entry.getValue();
if (StringUtils.equals(except, entry.getKey()) || !se.isOpen()) {
continue;
}
//noinspection SynchronizationOnLocalVariableOrMethodParameter
synchronized (se) {
try {
se.sendMessage(msg);
} catch (IOException e) {
LOGGER.error("[DrawGuess]发送消息\"{}\"出错", msg.getPayload(), e);
}
}
}
});
}
示例9: handleTextMessage
import org.springframework.web.socket.TextMessage; //导入依赖的package包/类
@Override
protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
Map<String, String> messageMap = getMessageMap(message);
if (messageMap.containsKey("type")) {
String type = messageMap.get("type");
switch (type) {
case "TERMINAL_INIT":
terminalService.onTerminalInit();
break;
case "TERMINAL_READY":
terminalService.onTerminalReady();
break;
case "TERMINAL_COMMAND":
terminalService.onCommand(messageMap.get("command"));
break;
case "TERMINAL_RESIZE":
terminalService.onTerminalResize(messageMap.get("columns"), messageMap.get("rows"));
break;
default:
throw new RuntimeException("Unrecodnized action");
}
}
}
示例10: sendMsg
import org.springframework.web.socket.TextMessage; //导入依赖的package包/类
public static void sendMsg() {
try {
List<TaskInfo> taskInfos = new LinkedList<>();
for (Entry<String, HttpDownInfo> downInfoEntry : HttpDownServer.DOWN_CONTENT.entrySet()) {
HttpDownInfo httpDownInfo = downInfoEntry.getValue();
if (httpDownInfo.getTaskInfo().getStatus() != 0) {
taskInfos.add(httpDownInfo.getTaskInfo());
}
}
TextMessage message = new TextMessage(JSON.toJSONString(taskInfos));
for (Entry<String, WebSocketSession> entry : HttpDownServer.WS_CONTENT.entrySet()) {
WebSocketSession session = entry.getValue();
if (session.isOpen()) {
synchronized (session) {
session.sendMessage(message);
}
}
}
} catch (Exception e) {
HttpDownServer.LOGGER.warn("sendMsg",e);
}
}
示例11: sendGroupChat
import org.springframework.web.socket.TextMessage; //导入依赖的package包/类
public int sendGroupChat(int userId, int notebookId, Date datetime, String content) {
//receive
String username = userInfoDao.getUserInfoById(userId).getUsername();
String avatar = userDao.getUserById(userId).getAvatar();
GroupChat groupChat = groupChatDao.getGroupChatById(notebookId);
Message msg = new Message(userId, username, avatar, notebookId, content, new Date());
//store
if(groupChat == null){
groupChatDao.addGroupChat(new GroupChat(notebookId, new ArrayList<String>()));
groupChat = groupChatDao.getGroupChatById(notebookId);
}
ArrayList<String> contents = groupChat.getContents();
JsonObject json = new JsonObject();
json.addProperty("uid", userId);
json.addProperty("datetime", datetime.toString());
json.addProperty("content", content);
contents.add(json.toString());
groupChat.setContents(contents);
groupChatDao.updateGroupChat(groupChat);
//broadcast
websocketHandler.sendMessageToGroup(userId, notebookId, new TextMessage(new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create().toJson(msg)));
return 0;
}
示例12: handleTextMessage
import org.springframework.web.socket.TextMessage; //导入依赖的package包/类
@Override
public void handleTextMessage(WebSocketSession session, TextMessage message) throws IOException {
String country = message.getPayload();
String reply="No data available";
if(country.equals("India"))
{
reply="DELHI";
}
else if(country.equals("USA"))
{
reply="Washington,D.C";
}
System.out.println("handling message");
for(WebSocketSession webSsession:sessions)
{
session.sendMessage(new TextMessage(reply));
}
}
示例13: handleTextMessage
import org.springframework.web.socket.TextMessage; //导入依赖的package包/类
@Override
public void handleTextMessage(WebSocketSession session, TextMessage message) {
try {
log.info("[{}] Processing {}", session.getId(), message);
SessionMetaData sessionMd = internalSessionMap.get(session.getId());
if (sessionMd != null) {
actorService.process(new TextPluginWebSocketMsg(sessionMd.sessionRef, message.getPayload()));
} else {
log.warn("[{}] Failed to find session", session.getId());
session.close(CloseStatus.SERVER_ERROR.withReason("Session not found!"));
}
session.sendMessage(message);
} catch (IOException e) {
log.warn("IO error", e);
}
}
示例14: send
import org.springframework.web.socket.TextMessage; //导入依赖的package包/类
@Override
public void send(PluginWebsocketMsg<?> wsMsg) throws IOException {
PluginWebsocketSessionRef sessionRef = wsMsg.getSessionRef();
String externalId = sessionRef.getSessionId();
log.debug("[{}] Processing {}", externalId, wsMsg);
String internalId = externalSessionMap.get(externalId);
if (internalId != null) {
SessionMetaData sessionMd = internalSessionMap.get(internalId);
if (sessionMd != null) {
if (wsMsg instanceof TextPluginWebSocketMsg) {
String payload = ((TextPluginWebSocketMsg) wsMsg).getPayload();
sessionMd.session.sendMessage(new TextMessage(payload));
}
} else {
log.warn("[{}][{}] Failed to find session by internal id", externalId, internalId);
}
} else {
log.warn("[{}] Failed to find session by external id", externalId);
}
}
示例15: handleTextMessage
import org.springframework.web.socket.TextMessage; //导入依赖的package包/类
@Override
public void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
FrameMessage frameMessage = objectMapper.readValue(message.getPayload(), FrameMessage.class);
List<BindedMethod> callbackList = messageCallbacks.get(frameMessage.getType());
Object messageData;
Class<?> messageType;
boolean isUnhandledMessage = true;
if (callbackList != null && 0 < callbackList.size())
for (BindedMethod method : callbackList) {
isUnhandledMessage = false;
messageType = method.getParameterTypes()[1];
messageData = objectMapper.treeToValue(frameMessage.getData(), messageType);
method.invoke(session, messageData);
}
if (isUnhandledMessage) {
String errorMessage = "Received message type is unhandled: " + frameMessage.getType();
throw new Exception(errorMessage);
}
}