【问题标题】:Java WebSocket session resulting to null after onOpenJava WebSocket 会话在 onOpen 后导致为空
【发布时间】:2022-02-10 20:15:38
【问题描述】:

我第一次在 javafx 项目中使用 websockets,当我启动程序时,会话被设置为局部变量会话,但是当我调用 sendMessage 函数后,会话又回到了 null。下面请找到我的客户类

package myclient;

import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javax.websocket.ClientEndpoint;
import javax.websocket.ContainerProvider;
import javax.websocket.DeploymentException;
import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.WebSocketContainer;

@ClientEndpoint
public class Client extends Application {    
    private static final Logger LOGGER = Logger.getLogger(Client.class.getName());
    private Session session;

    @OnOpen
    public void onOpen(Session session){
        this.session = session;
        System.out.println("Opened Session " + this.session);
    }

    @OnClose
    public void onClose(){
        System.out.println("Closed Session " + this.session);
    }

    @OnMessage
    public void onMessage(String msg){
        System.out.println("Websocket message received! " + msg);   
    }

    @Override
    public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("FXMLClient.fxml"));

        Scene scene = new Scene(root);

        connectToWebSocket();
        stage.setScene(scene);
        stage.show();
    }

    private void connectToWebSocket() {
        System.out.println("Client WebSocket initialized>>  " + this.session);
      WebSocketContainer container = ContainerProvider.getWebSocketContainer();
        try {
            URI uri = URI.create("ws://localhost:8080/Server/endpoint");
            container.connectToServer(this, uri);
        } 
        catch (DeploymentException | IOException ex) {
            LOGGER.log(Level.SEVERE, null, ex);
            System.exit(-1);
        }
    }

    public void sendMessage(String message) throws IOException{
        if(this.session != null){
                System.out.println(message + ", " + this.session);
                this.session.getBasicRemote().sendText(message);
        }
        else {
            System.out.println("Session is null");
        }
    }

    public static void main(String[] args) {
        launch(args);        
    }

}

有什么建议吗?

提前致谢

【问题讨论】:

  • 嘿,我不知道你的问题的解决方案,但至少你不再孤单了。我有同样的问题,但我没有找到任何解决方案(甚至四年后)......也许有人终于可以回答这个问题了吗?

标签: java session websocket null client


【解决方案1】:

我想我现在知道答案了。

您可能正在为此使用 tomcat 或其他服务器。当您在此答案中看到“tomcat”时,请插入您实际使用的服务器的名称。

当你的 websocket 连接打开时,tomcat 会自己创建一个 websocket 实例(你的Client)类。这意味着,onOpen-Method 将被调用,并且看起来好像是您创建了实例,打开了连接,而实际上 您没有。 Tomcat 做到了。

这反过来意味着,当您在客户端实例上调用 sendMessage 时,会话将是 null,因为该对象从未连接到任何地方。

哦,您无权访问由 tomcat 创建的已连接实例。

解决此问题的一种方法是在 onOpen-Method 中完成所有工作,但这并不实际。您可能希望将工作放在另一个方法中并从onOpen 调用它。这样,由 tomcat 创建的实例将完成必要的工作。

在我的项目中,我需要对 MQTT 主题进行投票并在网站上呈现数据(大学作业)。我在一个单独的类中进行了轮询,导致每当尝试使用我的sendMessage-method 发送接收到的数据时都会出现难以调试的错误。

我希望这个答案能稍微澄清一下,如果不是为了你,也许至少对于拥有相同大学作业的后代......

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-30
    • 2018-08-21
    相关资源
    最近更新 更多