{"record":{"id":"a45947190f679e67","repo":"TooTallNate/Java-WebSocket","slug":"this-method-must-be-used-in-conjunction-with-flush","errorCode":null,"errorMessage":"this method must be used in conjunction with flushAndClose","messagePattern":"this method must be used in conjunction with flushAndClose","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"src/main/java/org/java_websocket/WebSocketImpl.java","lineNumber":575,"sourceCode":"      this.wsl.onWebsocketClose(this, code, message, remote);\n    } catch (RuntimeException e) {\n\n      wsl.onWebsocketError(this, e);\n    }\n    if (draft != null) {\n      draft.reset();\n    }\n    handshakerequest = null;\n    readyState = ReadyState.CLOSED;\n  }\n\n  protected void closeConnection(int code, boolean remote) {\n    closeConnection(code, \"\", remote);\n  }\n\n  public void closeConnection() {\n    if (closedremotely == null) {\n      throw new IllegalStateException(\"this method must be used in conjunction with flushAndClose\");\n    }\n    closeConnection(closecode, closemessage, closedremotely);\n  }\n\n  public void closeConnection(int code, String message) {\n    closeConnection(code, message, false);\n  }\n\n  public synchronized void flushAndClose(int code, String message, boolean remote) {\n    if (flushandclosestate) {\n      return;\n    }\n    closecode = code;\n    closemessage = message;\n    closedremotely = remote;\n\n    flushandclosestate = true;\n","sourceCodeStart":557,"sourceCodeEnd":593,"githubUrl":"https://github.com/TooTallNate/Java-WebSocket/blob/afeacbf8c0f6f6a761c9d9daed8c813dd3b8ed7d/src/main/java/org/java_websocket/WebSocketImpl.java#L557-L593","documentation":"The no-argument closeConnection() can only be called after flushAndClose() has recorded the close code, message, and remote flag (closedremotely). Calling it standalone means the close parameters were never set, so the library throws IllegalStateException to prevent closing without a defined close handshake outcome.","triggerScenarios":"Calling websocket.closeConnection() (the parameterless overload) without first calling flushAndClose(code, message, remote) on the same connection.","commonSituations":"Server-side code that wants to forcibly close after flushing but calls closeConnection() instead of close(code, message); custom server implementations mixing the two close APIs.","solutions":["Use close(int code, String message) or closeConnection(int code, String message, boolean remote) instead of the parameterless closeConnection().","If you intentionally want flush-then-close, call flushAndClose(code, message, remote) first, then closeConnection().","For a normal graceful close just call websocket.close()."],"exampleFix":"// before\nconn.closeConnection();\n// after\nconn.close(1000, \"normal closure\");","handlingStrategy":"validation","validationCode":"// prefer explicit close parameters\ncode = 1000; message = \"normal closure\";","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use close(code, message) or closeConnection(code, message, remote) instead of the parameterless overload","Only pair closeConnection() after flushAndClose(code, message, remote)","Never call closeConnection() from an uninitialized state"],"tags":["illegal-state","close","lifecycle"],"backgroundTag":"invalid-state-transition","analyzedSha":"afeacbf8c0f6f6a761c9d9daed8c813dd3b8ed7d","analyzedAt":"2026-09-09T14:39:47.546Z","contentChangedAt":"2026-09-09T14:39:47.546Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}