{"id":19156913,"url":"https://github.com/bertrandmartel/websocket-java","last_synced_at":"2026-03-05T06:02:16.858Z","repository":{"id":31738258,"uuid":"35304267","full_name":"bertrandmartel/websocket-java","owner":"bertrandmartel","description":"Websocket Server/Client Java implementation","archived":false,"fork":false,"pushed_at":"2017-01-11T19:35:28.000Z","size":835,"stargazers_count":6,"open_issues_count":0,"forks_count":6,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-05-07T07:48:30.754Z","etag":null,"topics":["java-library","websocket"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bertrandmartel.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-05-08T22:14:06.000Z","updated_at":"2021-10-09T19:03:38.000Z","dependencies_parsed_at":"2022-08-29T14:00:56.258Z","dependency_job_id":null,"html_url":"https://github.com/bertrandmartel/websocket-java","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/bertrandmartel/websocket-java","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bertrandmartel%2Fwebsocket-java","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bertrandmartel%2Fwebsocket-java/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bertrandmartel%2Fwebsocket-java/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bertrandmartel%2Fwebsocket-java/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bertrandmartel","download_url":"https://codeload.github.com/bertrandmartel/websocket-java/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bertrandmartel%2Fwebsocket-java/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":30111779,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-05T03:40:26.266Z","status":"ssl_error","status_checked_at":"2026-03-05T03:39:15.902Z","response_time":93,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["java-library","websocket"],"created_at":"2024-11-09T08:36:23.346Z","updated_at":"2026-03-05T06:02:16.834Z","avatar_url":"https://github.com/bertrandmartel.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Websocket Java Server/Client implementation #\n\nhttp://bertrandmartel.github.io/websocket-java/\n\nWebsocket Server/Client implementation\n\nThis project uses HTTP endec project : http://bertrandmartel.github.io/http-endec-java/\n\n\u003ci\u003eLast update 16/05/2015\u003c/i\u003e\n\n* [16/05/2015] add SSL/TLS support\n* [09/05/2015] add Client event listener\n\nYou will find : \n* source in ./libwebsocket folder\n* secured and unsecured JS websocket client exemples featuring interactions with websocket server in ./exemples/js folder\n\n\u003chr/\u003e\n\n\u003cb\u003eHow to launch Websocket Server ?\u003c/b\u003e\n\n```\nWebsocketServer server = new WebsocketServer(WEBSOCKET_PORT);\nserver.start();\n```\n\nyou specify the port in WEBSOCKET_PORT\n\n\u003chr/\u003e\n\n\u003cb\u003eHow to monitor my clients connected to server ?\u003c/b\u003e\n\nJust add a Listener to server object. You have 3 callbacks that will notify you on client connection change and arrival of client messages\n\n```\nserver.addServerEventListener(new IClientEventListener() {\n\n\t\t\t@Override\n\t\t\tpublic void onMessageReceivedFromClient(IWebsocketClient client,\n\t\t\t\t\tString message) {\n\t\t\t\t//all your message received from websocket client will be here\n\t\t\t\tSystem.out.println(\"message received : \" + message);\n\t\t\t}\n\t\t\t\n\t\t\t@Override\n\t\t\tpublic void onClientConnection(IWebsocketClient client) {\n\t\t\t\t// when a websocket client connect. This will be called (you can store client object)\n\t\t\t\tSystem.out.println(\"Websocket client has connected\");\n\t\t\t\t\n\t\t\t\tclient.sendMessage(\"Hello,I'm a websocket server\");\n\t\t\t\t\n\t\t\t\t//client.close(); // this would close the client connection\n\t\t\t}\n\t\t\t\n\t\t\t@Override\n\t\t\tpublic void onClientClose(IWebsocketClient client) {\n\t\t\t\t// when a websocket client connection close. This will be called (you can dismiss client object)\n\t\t\t\tSystem.out.println(\"Websocket client has disconnected\");\n\t\t\t}\n\t\t});\n```\n\n\u003chr/\u003e\n\n\u003cb\u003eHow to launch a SSL secured websocket server ?\u003c/b\u003e\n\n```\nWWebsocketServer server = new WebsocketServer(WEBSOCKET_PORT);\n\nserver.setSsl(true); // set SSL to true (default is false)\n\n```\n\nThen you set your kestore, trustore, type of these certificates, filepath and passwords : \n\n```\nserver.setSSLParams(String KEYSTORE_DEFAULT_TYPE,\n\t\t    String KEYSTORE_FILE_PATH,\n\t\t    String TRUSTORE_FILE_PATH,\n\t\t    String SSL_PROTOCOL,\n\t\t    String KEYSTORE_PASSWORD,\n\t\t    String TRUSTORE_PASSWORD);\n```\n\nHere is the description of all of these parameters : \n\n* KEYSTORE_DEFAULT_TYPE : type of certificates used as keystore, it usually contains public and private certificates, common format are PKCS12 and JKS\n* TRUSTORE_DEFAULT_TYPE : type of certificates used as trustore, it should contain list of CA cert your server will trust\n* KEYSTORE_FILE_PATH : file path to keystore cert file\n* TRUSTORE_FILE_PATH: file path to trustore cert file\n* SSL_PROTOCOL : ssl protocol used \n* KEYSTORE_PASSWORD : keystore file password\n* TRUSTORE_PASSWORD : trustore file password\n\nEventually add event listener as described above and start websocket server : \n\n```\nserver.start();\n```\n\n\u003chr/\u003e\n\n\u003cb\u003eHow to connect a websocket client to server ?\u003c/b\u003e\n\nnew instance of websocket client to connect to server on HOSTNAME:PORT : \n\n```\nWebsocketClient clientSocket = new WebsocketClient(HOSTNAME, PORT);\n\nclientSocket.connect();\n```\n\nYou can monitor all your event in your websocket client adding a websocket client event listener like this :\n\n```\nclientSocket.addClientSocketEventListener(new IWebsocketClientEventListener() {\n\n\t\t\t\t\t@Override\n\t\t\t\t\tpublic void onSocketConnected() {\n\t\t\t\t\t\tSystem.out.println(\"[CLIENT] Websocket client successfully connected\");\n\t\t\t\t\t}\n\n\t\t\t\t\t@Override\n\t\t\t\t\tpublic void onSocketClosed() {\n\t\t\t\t\t\tSystem.out.println(\"[CLIENT] Websocket client disconnected\");\n\t\t\t\t\t}\n\n\t\t\t\t\t@Override\n\t\t\t\t\tpublic void onIncomingMessageReceived(byte[] data,\n\t\t\t\t\t\t\tIWebsocketClientChannel channel) {\n\t\t\t\t\t\tSystem.out.println(\"[CLIENT] Received message from server : \"+ new String(data));\n\t\t\t\t\t}\n\t\t\t\t});\n```\n* onSocketConnected() : happened when websocket client successfully connect to server\n* onSocketClosed()    : happened when websocket client disconnect from server\n* onIncomingMessageReceived(byte[] data,IWebsocketClientChannel channel) : happen when incoming data arrives from server. You can write a response directly via IWebsocketClientChannel object with ``writeMessage(String message)`` method\n\nWebsocket client can be enabled with SSL :\n\n\n```\n// set SSL encryption\nclientSocket.setSsl(true);\n\n// set ssl parameters\n\nclientSocket.setSSLParams(KEYSTORE_DEFAULT_TYPE, TRUSTORE_DEFAULT_TYPE,\n\t\tCLIENT_KEYSTORE_FILE_PATH, CLIENT_TRUSTORE_FILE_PATH,\n\t\tSSL_PROTOCOL, KEYSTORE_PASSWORD, TRUSTORE_PASSWORD);\n\n```\nexactly the same as websocket server\n\n\u003chr/\u003e\n\n\u003cb\u003eKeystore : public and private server certificates\u003c/b\u003e\n\n* To convert cert and key certs to p12 : \n\n``openssl pkcs12 -export -out server.p12 -inkey server.key -in server.crt``\n\nThus, you will have : ``String KEYSTORE_DEFAULT_TYPE = \"PKCS12\"``\n\n* To convert your p12 (containing public and private cert) to jks : \n\nYou have to know your alias (name matching your cert entry), if you dont have it retrieve it with : ``keytool -v -list -storetype pkcs12 -keystore server.p12``\n\n``keytool -importkeystore -srckeystore server.p12 -srcstoretype PKCS12 -deststoretype JKS -destkeystore server.jks``\n\nThus, you will have : ``String KEYSTORE_DEFAULT_TYPE = \"JKS\"``\n\n\u003cb\u003eTrustore : should contain only CA certificates\u003c/b\u003e\n\nconvert ca cert to jks : \n\n```keytool -import -alias ca -file ca.crt -keystore cacerts.jks -storepass 123456```\n\nThus, you will have : ``String TRUSTORE_DEFAULT_TYPE = \"JKS\"``\n\n\u003chr/\u003e\n\n\u003cb\u003eTroubleShooting\u003c/b\u003e\n\n\u003ci\u003eBad certificate | Unknown CA errors\u003c/i\u003e\n\nThis could mean you didn't import your not-trusted-CA certificate into your browser.\n\n\u003ci\u003eThe remote host closed the connection\u003c/i\u003e\n\nUsually happen when your browser closed the connection before the end of SSL handshake. If you already added your CA to your browser dont worry.\nBoth Chrome and Firefox need to MANUALLY add the certificate (in a popup) so putting it in parameter menu don't change anything.\n\nJust load your URL with \"https\" : https://127.0.0.1:8443 . Browser will prompt you to accept the certificates and it will probably solve your connection error.\n\n\u003ci\u003eCKR_DOMAIN_PARAMS_INVALID error using openjdk\u003c/i\u003e\n\nWith openjdk-6-jdk and openjdk-7-jdk, I encountered java.security bug described in https://bugs.launchpad.net/ubuntu/+source/openjdk-7/+bug/1006776 triggering a ``CKR_DOMAIN_PARAMS_INVALID`` exception error. Solution was to edit java.security parameters in /etc/java-7-openjdk/security/java.security \n\nI replaced that : \n```\nsecurity.provider.9=sun.security.smartcardio.SunPCSC\nsecurity.provider.10=sun.security.pkcs11.SunPKCS11 ${java.home}/lib/security/nss.cfg\n```\n\nwith that : \n```\n#security.provider.9=sun.security.smartcardio.SunPCSC\nsecurity.provider.9=sun.security.ec.SunEC\n#security.provider.10=sun.security.pkcs11.SunPKCS11 ${java.home}/lib/security/nss.cfg\n```\n\n\u003cb\u003eBrowser tested\u003c/b\u003e\n\nThis has been tested on following browser : \n* Chrome\n* Chromium\n* Firefox\n\n\u003chr/\u003e\n\n\u003cb\u003eDebugging SSL connection error\u003c/b\u003e\n\nI recommmend using openssl command line tool to debug ssl connection : \n\n``openssl s_client -connect 127.0.0.1:8443``\n\nYou can also use vm argument debugging java ssl implementation : ``-Djavax.net.debug=ssl``\n\n\u003chr/\u003e\n\n\u003cb\u003eServer-Client key/cert generation\u003c/b\u003e\n\nCerts are in libwesocket-test/certs folder, you will find server,client and ca cert build with easy-rsa :\n\nhttps://github.com/OpenVPN/easy-rsa\n\nWith last release of easy-rsa, you can build your own key with the following : \n\n* ``./build-ca`` : generate a new CA for you\n* ``./build-server-full myServer`` : will build for you public cert and private cert signed with CA for server\n* ``./build-client-full myClient`` : will build for you public cert and private cert signed with CA for client\n\n\u003cb\u003eHow to close my websocket server ?\u003c/b\u003e\n\n``server.closeServer();``\n\n\u003chr/\u003e\n\n\u003cb\u003eCOMMAND LINE SYNTAX\u003c/b\u003e \n\nThe following will open a websocket on port 4343 (default port value for my exemple)\n\n``java -cp ../libs/commons-codec-1.9.jar:../libs/http-endec-1.0.jar:libwebsocket-1.0.jar fr.bmartel.websocket.LaunchServer``\n\nYou can change port number by specifying yours\n\n``java -cp ../libs/commons-codec-1.9.jar:../libs/http-endec-1.0.jar:libwebsocket-1.0.jar fr.bmartel.websocket.LaunchServer 4343``\n\nThis exemple is launched from /release folder\n\n\u003chr/\u003e\n\n\u003cb\u003eExemple with Javascript Client\u003c/b\u003e\n\n* Launch the websocket server on port 4242\n* Open the javascript client page in ./exemples/js/ folder\n\n=\u003e You have now a complete websocket chat between java server \u003c-\u003e javascript client in websocket \n\n![client side](https://raw.github.com/bertrandmartel/websocket-java/master/exemples/readme_images/clientSide.png)\n\n\n![server side](https://raw.github.com/bertrandmartel/websocket-java/master/exemples/readme_images/serverSide.png)\n\n\n\u003cb\u003eExemple with Java websocket client \u003c-\u003e Java websocket server\u003c/b\u003e\n\n* Launch the LaunchClient main class from folder libwebsocket in fr.bmartel.network package\n\nThis will launch one websocket server on 127.0.0.1:8443 and connect a websocket client to it.\nYou will be able to send message to the server via websocket and server will answer you back :\n\n![wesocket client exemple](https://raw.github.com/bertrandmartel/websocket-java/master/exemples/readme_images/websocket_client.png)\n\n\u003chr/\u003e\n\n* Project is JRE 1.7 compliant\n* You can build it with ant =\u003e build.xml\n* Development on Eclipse \n* Specification from https://tools.ietf.org/html/rfc6455\n\nSoon : an exemple using this lib as websocket client communicating with a cpp server\u003cbr/\u003e\nSoon : websocket client exemple\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbertrandmartel%2Fwebsocket-java","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbertrandmartel%2Fwebsocket-java","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbertrandmartel%2Fwebsocket-java/lists"}