简体   繁体   English

使用 wss (websocket) 发送和接收数据的格式

[英]Format to send and receive data using wss (websocket)

I am working on Websocket currently.我目前正在研究 Websocket。 So do I send and receive data using wss protocol?那么我是否使用 wss 协议发送和接收数据? I am already using HTTP post and get but need to upgrade to wss.我已经在使用 HTTP post 和 get 但需要升级到 wss。 Please help.请帮忙。 Thanks in advance提前致谢

if you are using standalone applications (console app) I recommend you to use java-websocket or if it's a JavaEE WebApp, Example:如果您使用的是独立应用程序(控制台应用程序),我建议您使用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));

        }
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM