半自定义大剑仙 2023-09-26 09:30 采纳率: 50%
浏览 8

java,websocket

我在本地使用java代码编写一个webSocket服务,前端使用VUE进行调用,在本地能正常进行连接。我将代码打包放在linux上运行,但是前端不能正常进行连接。
后台引入的pom文件

    <!--webSocket-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-websocket</artifactId>
            <version>3.1.4</version>
        </dependency>

报错截图

img

  • 写回答

1条回答 默认 最新

  • CSDN-Ada助手 CSDN-AI 官方账号 2023-09-26 10:05
    关注

    【相关推荐】



    • 你可以参考下这个问题的回答, 看看是否对你有帮助, 链接: https://ask.csdn.net/questions/4646641
    • 我还给你找了一篇非常好的博客,你可以看看是否有帮助,链接:Vue+Java 通过websocket实现服务器与客户端双向通信
    • 除此之外, 这篇博客: java实现WebSocket服务端中的 三个类,简单实现WebSocket服务端 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:

      -首先引入jar包

      <!-- https://mvnrepository.com/artifact/org.java-websocket/Java-WebSocket -->
              <dependency>
                  <groupId>org.java-websocket</groupId>
                  <artifactId>Java-WebSocket</artifactId>
                  <version>1.3.7</version>
              </dependency>
      

      MsgWebSocketServer

      
      import java.net.InetSocketAddress;
      import java.util.Iterator;
      
      import com.alibaba.fastjson.JSONObject;
      import org.java_websocket.WebSocket;
      import org.java_websocket.handshake.ClientHandshake;
      import org.java_websocket.server.WebSocketServer;
      
      
      /**
       * websocket服务器
       */
      public class MsgWebSocketServer extends WebSocketServer{
      
          public MsgWebSocketServer(int port) {
              super(new InetSocketAddress(port));
          }
          /**
           * WebSocket连接关闭时调用
           */
          @Override
          public void onClose(WebSocket ws, int arg1, String arg2, boolean arg3) {
              System.out.println("------------------onClose-------------------");
          }
      
          /**
           * 错误发生时调用。
           */
          @Override
          public void onError(WebSocket ws, Exception e) {
              System.out.println("------------------onError-------------------");
              if(ws != null) {
              }
              e.getStackTrace();
          }
      
          /**
           * 接收到的消息
           */
          @Override
          public void onMessage(WebSocket ws, String msg) {
              System.out.println("收到消息:"+msg);
              //收到什么消息,回复什么
              ws.send(msg);
              if(ws.isClosed()) {
              } else if (ws.isClosing()) {
                  System.out.println("ws连接正在关闭...");
              } else if (ws.isConnecting()) {
                  System.out.println("ws正在连接中...");
              } else if(ws.isOpen()) {
                  System.out.println("ws连接已打开...");
                  System.out.println(msg);
              }
          }
      
          /**
           * websocket进行握手之后调用,并且给WebSocket写做准备
           * 通过握手可以获取请求头信息
           */
          @Override
          public void onOpen(WebSocket ws, ClientHandshake shake) {
              System.out.println("-----------------onOpen--------------------"+ws.isOpen()+"--"+ws.getReadyState()+"--"+ws.getAttachment());
              for(Iterator<String> it=shake.iterateHttpFields();it.hasNext();) {
                  String key = it.next();
                  System.out.println(key+":"+shake.getFieldValue(key));
              }
          }
          /**
           * 当服务器成功启动时调用
           */
          @Override
          public void onStart() {
              System.out.println("------------------onStart-------------------");
          }
      }
      
      

      WebServerEnum

      
      public enum WebServerEnum {
      
          server;
      
          private static MsgWebSocketServer socketServer = null;
      
          public static void init(MsgWebSocketServer server) {
              socketServer = server;
              if (socketServer != null) {
                  socketServer.start();
              }
          }
      }
      

      启动类

      public class SocketServerEngine {
          public static void main(String[] args) {
              WebServerEnum.server.init(new MsgWebSocketServer(8090));
          }
      }
      

      连接地址: ws://localhost:8090


    如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^
    评论

报告相同问题?

问题事件

  • 创建了问题 9月26日

悬赏问题

  • ¥66 关于川崎机器人调速问题
  • ¥15 winFrom界面无法打开
  • ¥15 crossover21 ARM64版本安装软件问题
  • ¥15 mymetaobjecthandler没有进入
  • ¥15 mmo能不能做客户端怪物
  • ¥15 osm下载到arcgis出错
  • ¥15 Dell g15 每次打开eiq portal后3分钟内自动退出
  • ¥200 使用python编写程序,采用socket方式获取网页实时刷新的数据,能定时print()出来就行。
  • ¥15 matlab如何根据图片中的公式绘制e和v的曲线图
  • ¥15 我想用Python(Django)+Vue搭建一个用户登录界面,但是在运行npm run serve时报错了如何解决?