publicObjectcreateWebSocket(JettyServerUpgradeRequestreq,JettyServerUpgradeResponseresp){StringmethodName="createWebSocket";StringremoteSystemName=null;StringremoteUuid="";booleanrequestSecure=req.isSecure();if(this.useSsl&&!requestSecure){this.logger.debug("createWebSocket","Incoming insecure websocket upgrade request is not allowed (SSL / TLS is required in settings)");returnthis.sendError(resp,403,"Bad scheme");}else{HttpServletRequesthttpServletRequest=req.getHttpServletRequest();intrequestPort=httpServletRequest.getLocalPort();Stringprotocol;intlocalPort;if(requestSecure){protocol="https";localPort=this.localHttpsPort;}else{protocol="http";localPort=this.localHttpPort;}if(requestPort!=localPort){this.logger.debug("createWebSocket",String.format("Incoming %s request port %d is not allowed (expected %d)",protocol,requestPort,localPort));returnthis.sendError(resp,403,"Bad port");}else{Map<String,List<String>>params=req.getParameterMap();List<String>nameParts=(List)params.get("name");if(nameParts.isEmpty()){this.logger.error("createWebSocket",String.format("Request parameter '%s' was not sent during web socket connect request","name"),(Throwable)null);}else{remoteSystemName=(String)nameParts.get(0);}List<String>urlParts=(List)params.get("url");StringremoteAddr;if(urlParts.isEmpty()){remoteAddr=String.format("Request parameter '%s' was not sent during web socket connect request","url");this.logger.error("createWebSocket",remoteAddr,(Throwable)null);returnthis.sendError(resp,400,remoteAddr);}else{StringremoteSystemUrlStr=(String)urlParts.get(0);remoteAddr=httpServletRequest.getRemoteAddr();if(!remoteSystemUrlStr.contains(remoteAddr)){String[]split=remoteSystemUrlStr.split(":");remoteSystemUrlStr=split[0]+"://"+remoteAddr+":"+split[2];}URLremoteSystemUrl;try{remoteSystemUrl=newURL(remoteSystemUrlStr);}catch(MalformedURLExceptionvar22){this.logger.error("createWebSocket",String.format("The URL request parameter '%s' is not a valid URL",remoteSystemUrlStr),(Throwable)null);returnthis.sendError(resp,400,"The URL request parameter is not a valid URL");}List<String>uuidParts=(List)params.get("uuid");if(uuidParts!=null&&!uuidParts.isEmpty()){remoteUuid=(String)uuidParts.get(0);}this.logger.debug("createWebSocket",String.format("Incoming connection from: '%s', remoteSystemName='%s', uuid='%s'",remoteSystemUrl,remoteSystemName,remoteUuid));RemoteSystemIdremoteSystemId=StringUtils.isBlank(remoteUuid)?newRemoteSystemIdURL(remoteSystemUrlStr,remoteSystemName):newRemoteSystemIdUUID(remoteUuid,remoteSystemName);if(this.connectionSecurityPlugin!=null){StringsecurityMsg=this.connectionSecurityPlugin.checkConnection((RemoteSystemId)remoteSystemId,String.valueOf(remoteSystemUrl));if(securityMsg.startsWith("SecurityFail:")){this.logger.debug("onConnect",securityMsg);try{resp.sendForbidden("Approval required");}catch(IOExceptionvar21){}returnnull;}}MetroWebSocketnewReceiver=newMetroWebSocket(this,(RemoteSystemId)remoteSystemId,remoteSystemUrl,Direction.Incoming,requestSecure);RemoteSystemIdUUIDlocalId=newRemoteSystemIdUUID(this.getLocalSystemUUID(),this.getLocalSystemId());resp.setHeader("remoteSystemId",localId.toString());returnnewReceiver;}}}}
<script>letsocket=newWebSocket("ws://172.16.1.152:8088/system/ws-control-servlet?name=q&uuid=6a7e39e1-1ca4-405f-bfb3-6d971d6e7211&url=http://172.16.1.152:8088/system");socket.onopen=function(e){alert("[open] Connection established");socket.send("My name is John");};socket.onmessage=function(event){alert(`[message] Data received from server: ${event.data}`);};socket.onclose=function(event){if(event.wasClean){alert(`[close] Connection closed cleanly, code=${event.code} reason=${event.reason}`);}else{// 例如服务器进程被杀死或网络中断
// 在这种情况下,event.code 通常为 1006
alert('[close] Connection died');}};socket.onerror=function(error){alert(`[error] ${error.message}`);};</script>