程序员王小瑞 2023-04-21 14:49 采纳率: 19.2%
浏览 12

websoket相关问题

App经过网关转发websocket请求,app发送连接请求是正常的,app应用手动发送断开连接是正常的,app应用切换到后台时自动关闭websocket连接请求时网关报错,但也走了websocket服务端onclose方法连接正常关闭了,就是网关老报错。

个人感觉应该跟网关配置那块有问题

spring:
  application:
    name: sc-gateway
  main:
    allow-bean-definition-overriding: true
  cloud:
    gateway:
      discovery:
        locator:
          enabled: false                     ###参考网上资料是不是不应该配置
          lower-case-service-id: true
      globalcors:
        corsConfigurations:
          '[/**]':
            allowedOrigins: "docs.spring.io"
            allowedMethods:
              - GET
                POST
                DELETE
                PUT
                OPTION
      routes:
   #mini-mes基础服务
      - id: mini-mes
        order: 3
        uri: lb://mini-mes
        predicates:
          - Path=/api-i/**
        filters:
          - StripPrefix=1
          
     #mini-mes的webscoket接口
      - id: mini-mes-socket
        order: 2
        uri: lb:ws://mini-mes
        predicates:
          - Path=/api-i/webScoket/**
        filters:
          - StripPrefix=1
          - RemoveRequestHeader= SESSION

线上错误

```java
2023-04-21 14:19:38.273 ERROR 1 --- [-server-epoll-5] .a.w.r.e.DefaultErrorWebExceptionHandler : Failed to handle request [GET http://www.ccc.com:8766/api-i/webScoket/82-SA0003-appSendData]

io.netty.channel.unix.Errors$NativeIoException: syscall:read(..) failed: Connection reset by peer
    at io.netty.channel.unix.FileDescriptor.readAddress(..)(Unknown Source)

2023-04-21 14:19:38.274 ERROR 1 --- [-server-epoll-5] o.s.w.s.adapter.HttpWebHandlerAdapter    : Failed to handle request [GET http://www.ccc.com:8766/api-i/webScoket/82-SA0003-appSendData]

java.lang.IllegalStateException: Status and headers already sent
    at reactor.ipc.netty.http.server.HttpServerOperations.status(HttpServerOperations.java:346)
    at org.springframework.http.server.reactive.ReactorServerHttpResponse.applyStatusCode(ReactorServerHttpResponse.java:67)
    at org.springframework.http.server.reactive.AbstractServerHttpResponse.lambda$null$4(AbstractServerHttpResponse.java:214)
    at reactor.core.publisher.MonoRunnable.subscribe(MonoRunnable.java:42)
    at reactor.core.publisher.Mono.subscribe(Mono.java:3080)
    at reactor.core.publisher.FluxConcatIterable$ConcatIterableSubscriber.onComplete(FluxConcatIterable.java:147)
    at reactor.core.publisher.FluxConcatIterable.subscribe(FluxConcatIterable.java:61)
    at reactor.core.publisher.MonoIgnoreElements.subscribe(MonoIgnoreElements.java:37)
    at reactor.core.publisher.Mono.subscribe(Mono.java:3080)
    at org.springframework.http.server.reactive.ChannelSendOperator$WriteBarrier.onNext(ChannelSendOperator.java:181)
    at reactor.core.publisher.FluxPeekFuseable$PeekFuseableSubscriber.onNext(FluxPeekFuseable.java:198)
    at reactor.core.publisher.FluxMapFuseable$MapFuseableSubscriber.onNext(FluxMapFuseable.java:115)
    at reactor.core.publisher.FluxJust$WeakScalarSubscription.request(FluxJust.java:99)
    at reactor.core.publisher.FluxMapFuseable$MapFuseableSubscriber.request(FluxMapFuseable.java:156)
    at reactor.core.publisher.FluxPeekFuseable$PeekFuseableSubscriber.request(FluxPeekFuseable.java:138)
    at org.springframework.http.server.reactive.ChannelSendOperator$WriteBarrier.onSubscribe(ChannelSendOperator.java:163)

线下错误

2023-04-21 14:30:05.170 ERROR 7904 --- [ctor-http-nio-7] r.i.n.channel.CloseableContextHandler    : Handler failure while no child channelOperation was present

java.io.IOException: 远程主机强迫关闭了一个现有的连接。
    at sun.nio.ch.SocketDispatcher.read0(Native Method)
    at sun.nio.ch.SocketDispatcher.read(SocketDispatcher.java:43)
    at sun.nio.ch.IOUtil.readIntoNativeBuffer(IOUtil.java:223)
    at sun.nio.ch.IOUtil.read(IOUtil.java:192)
    at sun.nio.ch.SocketChannelImpl.read(SocketChannelImpl.java:380)
    at io.netty.buffer.PooledUnsafeDirectByteBuf.setBytes(PooledUnsafeDirectByteBuf.java:288)
    at io.netty.buffer.AbstractByteBuf.writeBytes(AbstractByteBuf.java:1108)
    at io.netty.channel.socket.nio.NioSocketChannel.doReadBytes(NioSocketChannel.java:345)
    at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:148)
    at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:647)
    at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:582)
    at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:499)
    at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:461)
    at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:884)
    at java.lang.Thread.run(Thread.java:748)

2023-04-21 14:30:05.187 ERROR 7904 --- [ctor-http-nio-7] .a.w.r.e.DefaultErrorWebExceptionHandler : Failed to handle request [GET http://192.168.3.5:8766/api-i/webScoket/20-SA0003-appSendData]

java.io.IOException: 远程主机强迫关闭了一个现有的连接。
    at sun.nio.ch.SocketDispatcher.read0(Native Method)
    at sun.nio.ch.SocketDispatcher.read(SocketDispatcher.java:43)
    at sun.nio.ch.IOUtil.readIntoNativeBuffer(IOUtil.java:223)
    at sun.nio.ch.IOUtil.read(IOUtil.java:192)
    at sun.nio.ch.SocketChannelImpl.read(SocketChannelImpl.java:380)
    at io.netty.buffer.PooledUnsafeDirectByteBuf.setBytes(PooledUnsafeDirectByteBuf.java:288)
    at io.netty.buffer.AbstractByteBuf.writeBytes(AbstractByteBuf.java:1108)
    at io.netty.channel.socket.nio.NioSocketChannel.doReadBytes(NioSocketChannel.java:345)
    at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:148)
    at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:647)
    at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:582)
    at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:499)
    at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:461)
    at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:884)
    at java.lang.Thread.run(Thread.java:748)

2023-04-21 14:30:05.206 ERROR 7904 --- [ctor-http-nio-7] o.s.w.s.adapter.HttpWebHandlerAdapter    : Failed to handle request [GET http://192.168.3.5:8766/api-i/webScoket/20-SA0003-appSendData]

java.lang.IllegalStateException: Status and headers already sent
    at reactor.ipc.netty.http.server.HttpServerOperations.status(HttpServerOperations.java:346)
    at org.springframework.http.server.reactive.ReactorServerHttpResponse.applyStatusCode(ReactorServerHttpResponse.java:67)
    at org.springframework.http.server.reactive.AbstractServerHttpResponse.lambda$null$4(AbstractServerHttpResponse.java:214)
    at reactor.core.publisher.MonoRunnable.subscribe(MonoRunnable.java:42)
    at reactor.core.publisher.Mono.subscribe(Mono.java:3080)
    at reactor.core.publisher.FluxConcatIterable$ConcatIterableSubscriber.onComplete(FluxConcatIterable.java:147)
    at reactor.core.publisher.FluxConcatIterable.subscribe(FluxConcatIterable.java:61)
    at reactor.core.publisher.MonoIgnoreElements.subscribe(MonoIgnoreElements.java:37)

```

  • 写回答

2条回答 默认 最新

  • CSDN-Ada助手 CSDN-AI 官方账号 2023-04-21 16:06
    关注
    • 这篇博客: webSocket部署到远程服务器遇到的坑中的 webSoket连接部分源码 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
    • 前台

        var websocket = null;
          var Ip = window.location.host;
        if ('WebSocket' in window) {
               websocket = new WebSocket("ws://"+Ip+"/ws/dkuserSocket?userId="+"admin");
          }
          else
          {
              alert('当前浏览器 Not support websocket');
           }
      
          // 连接发生错误的回调方法
          websocket.onerror = function () {
              console.log("webSocke连接错误");
          };
      
          // 连接成功建立的回调方法
          websocket.onopen = function () {
      
              var param={
                  "billId":billId,
                  "billType":billType
              };
              
                  // 接收到消息的回调方法
          websocket.onmessage = function (event) {
          }
          
          
          // 连接关闭的回调方法
          websocket.onclose = function () {
      
              }
      
          // 监听窗口关闭事件,当窗口关闭时,主动去关闭websocket连接,防止连接还没断开就关闭窗口,server端会抛异常。
          window.onunload = function () {
                  closeWebSocket();
              }
      
          // 关闭WebSocket连接
          function closeWebSocket() {
                  websocket.close();
              }
              
      
    评论

报告相同问题?

问题事件

  • 创建了问题 4月21日

悬赏问题

  • ¥30 不会,学习,有偿解答
  • ¥15 SQL查询语句报错(检查)
  • ¥15 此表中公式应该怎么写
  • ¥15 求HI-TECH PICC 9.50 PL3安装包
  • ¥15 下载ctorch报错,求解
  • ¥15 如何入门学习c语言,单片机
  • ¥15 idea 编辑语言的选择
  • ¥15 Windows下部署Asmjit
  • ¥15 请问双层规划模型的上下层目标函数不一致,是如何保证迭代收敛性的
  • ¥30 微信小程序 前端页面内容搜索