Sai*_*deh 5 java websocket atmosphere reactjs atmosphere.js
我正在构建一个反应应用程序,它使用气氛库来监听SpringBoot websocket,当客户端尝试连接到服务器时,它会在控制台中抛出一个错误,并显示Some cookies are misusing the recommended \xe2\x80\x9csameSite\xe2\x80\x9c attribute。我向对象添加了一些属性request来按照建议修复问题( SameSite cookies)。但我仍然遇到同样的错误。
ReactJS代码:
\nimport React from \'react\';\nimport * as atmosphere from \'atmosphere.js\';\n\n//import $ from \'jquery\';\nvar transport = \'websocket\';\n//var req = new atmosphere.AtmosphereRequest();\n\n// We are now ready to cut the request\nvar request = {\n url:\'http://localhost:8080/stream\',\n contentType: "application/json",\n trackMessageLength: true,\n shared: true,\n enableXDR: true,\n headers: { \'Access-Control-Allow-Origin\': \'*\',\n \'sameSite\': \' None; Secure\'\n },\n //sameSite: \'None; Secure\',\n rewriteURL:true,\n transport: transport,\n fallbackTransport: \'long-polling\',\n onOpen: function(response:any) {\n console.log(\'Atmosphere connected using \' , response.transport);\n transport = response.transport;\n },\n onTransportFailure: function(errorMsg: Atmosphere.Response, request: Atmosphere.Request) {\n console.log(\'Atmosphere Chat. Default transport is WebSocket, fallback is \' ,request.fallbackTransport );\n },\n onMessage: function (response:Atmosphere.Response) {\n\n var message = response.responseBody;\n try {\n console.log(\'message: \', message);\n } catch (e) {\n console.log(\'This doesn\\\'t look like a valid JSON: \', message);\n return;\n }\n },\n onClose : function(response: Atmosphere.Response) {\n console.log("Close connection !!!");\n }\n \n};\n\nconst socket = atmosphere;\n// Connect to the server, hook up to the request handler.\nconsole.log(\'socket : \', socket.subscribe);\nsocket.subscribe && socket.subscribe(request);\n\nconst AtmosphereWebSocket = () => {\n return ( <div> </div> );\n}\n\nexport default AtmosphereWebSocket;Run Code Online (Sandbox Code Playgroud)\r\nSpringBoot代码:
\n@Component\n@CrossOrigin(origins = "http://localhost:3000")\n@WebSocketHandlerService(path = "/stream", broadcaster = SimpleBroadcaster.class,\n atmosphereConfig = {"org.atmosphere.websocket.WebSocketProtocol=" +\n "org.atmosphere.websocket.protocol.StreamingHttpProtocol"})\npublic class WebSocketStream extends WebSocketStreamingHandlerAdapter {\n\n private final Logger logger = LoggerFactory.getLogger(WebSocketStream.class);\n\n public WebSocketStream() {\n System.out.println(" ** WebSocketStream ** ");\n }\n\n // A thread which sends a stream of data out of a websocket. Create when the class\n // is instantiated, inject the websocket when open.\n private class Stream extends Thread {\n protected WebSocket socket;\n protected final ObjectMapper mapper = new ObjectMapper();\n protected boolean stop = false;\n\n public Stream(WebSocket socket) {\n this.socket = socket;\n }\n\n public void run() {\n int count = 0;\n try {\n while (!stop) {\n Map<String, Object> message = new HashMap<String, Object>();\n message.put("time", new Date().toString());\n message.put("count", count++);\n String string = mapper.writeValueAsString(message);\n socket.write(string);\n System.out.println("tick: " + string);\n Thread.sleep(1000);\n }\n } catch (Exception x) {\n // break.\n }\n }\n }\n\n int clients = 0;\n\n @Override\n public void onOpen(WebSocket webSocket) throws IOException {\n // Hook up the stream.\n final Stream stream = new Stream(webSocket);\n stream.start();\n logger.info(" on open was called !!!");\n webSocket.broadcast("client " + clients++ + " connected");\n webSocket.resource().addEventListener(new WebSocketEventListenerAdapter() {\n @Override\n public void onDisconnect(AtmosphereResourceEvent event) {\n if (event.isCancelled()) {\n logger.info("Browser {} unexpectedly disconnected", event.getResource().uuid());\n } else if (event.isClosedByClient()) {\n logger.info("Browser {} closed the connection", event.getResource().uuid());\n }\n stream.stop = true;\n }\n });\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n错误信息:
\nWebsocket failed on first connection attempt. Downgrading to long- polling and resending 1.chunk.js:3632:18\nAtmosphere Chat. Default transport is WebSocket, fallback is long-polling atmosphere.tsx:27\nThe development server has disconnected.\nRefresh the page if necessary. 1.chunk.js:7419:13\nSat Jul 11 2020 15:52:07 GMT-0500 (Central Daylight Time) Atmosphere: unload event 1.chunk.js:3632:18\n[HMR] Waiting for update signal from WDS... log.js:24\nDownload the React DevTools for a better development experience: react-dom.development.js:24994\nsocket : \nfunction subscribe(url, callback, request)\natmosphere.tsx:47\nFirefox can\xe2\x80\x99t establish a connection to the server at ws://localhost:8080/stream?X-Atmosphere-tracking-id=0&X-Atmosphere-Framework=3.0.5-javascript&X-Atmosphere-Transport=websocket&X-Atmosphere-TrackMessageSize=true&Content-Type=application/json&X-atmo-protocol=true&Access-Control-Allow-Origin=*&sameSite=%20None%3B%20Secure. atmosphere.js:1201\nWebsocket closed, reason: Connection was closed abnormally (that is, with no close frame being sent). - wasClean: false atmosphere.js:3302\nClose connection !!! atmosphere.tsx:40\nWebsocket failed on first connection attempt. Downgrading to long-polling and resending atmosphere.js:3302\nAtmosphere Chat. Default transport is WebSocket, fallback is long-polling\nRun Code Online (Sandbox Code Playgroud)\n