使用wss(websocket)格式发送和接收数据
Format to send and receive data using wss (websocket)
我目前正在研究 Websocket。那么我是不是使用wss协议收发数据呢?我已经在使用 HTTP post 并获得但需要升级到 wss。请帮忙。提前致谢
如果您使用的是独立应用程序(控制台应用程序),我建议您使用 java-websocket 或者如果它是 JavaEE WebApp,示例:
package org.hectorvent.gpstracking.websocket;
import org.hectorvent.gpstracking.restful.model.PdaGeoData;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.Future;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.Singleton;
import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
@Singleton
@ServerEndpoint("/geodata")
public class WebSocketGmap {
private final Set<Session> clients = new HashSet();
@OnOpen
public void open(Session session) {
clients.add(session);
}
@OnMessage
public void onMessage(String message, Session session) {
// here you're going to received client messages.
}
@OnClose
public void close(Session session) {
clients.remove(session);
}
@OnError
public void onError(Throwable error) {
}
public void sendMessage(PdaGeoData pgd) {
for (Session client : clients) {
Future fu = client.getAsyncRemote()
.sendText(GsonUtils.toJson(pgd));
}
}
}
我目前正在研究 Websocket。那么我是不是使用wss协议收发数据呢?我已经在使用 HTTP post 并获得但需要升级到 wss。请帮忙。提前致谢
如果您使用的是独立应用程序(控制台应用程序),我建议您使用 java-websocket 或者如果它是 JavaEE WebApp,示例:
package org.hectorvent.gpstracking.websocket;
import org.hectorvent.gpstracking.restful.model.PdaGeoData;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.Future;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.Singleton;
import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
@Singleton
@ServerEndpoint("/geodata")
public class WebSocketGmap {
private final Set<Session> clients = new HashSet();
@OnOpen
public void open(Session session) {
clients.add(session);
}
@OnMessage
public void onMessage(String message, Session session) {
// here you're going to received client messages.
}
@OnClose
public void close(Session session) {
clients.remove(session);
}
@OnError
public void onError(Throwable error) {
}
public void sendMessage(PdaGeoData pgd) {
for (Session client : clients) {
Future fu = client.getAsyncRemote()
.sendText(GsonUtils.toJson(pgd));
}
}
}