{"id":37098731,"url":"https://github.com/tsingson/fastws-example","last_synced_at":"2026-01-14T12:00:43.031Z","repository":{"id":50780981,"uuid":"231438465","full_name":"tsingson/fastws-example","owner":"tsingson","description":"golang / javascript websocket example via flatbuffers , fasthttp,  fastws","archived":false,"fork":false,"pushed_at":"2021-05-29T22:52:26.000Z","size":903,"stargazers_count":1,"open_issues_count":1,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-10-21T07:59:59.633Z","etag":null,"topics":["fasthttp","fastws","flatbuffers","golang","javascript","router","websocket"],"latest_commit_sha":null,"homepage":"https://tsingson.github.io","language":"JavaScript","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/tsingson.png","metadata":{"files":{"readme":"README-cn.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-01-02T18:34:59.000Z","updated_at":"2024-06-12T06:53:04.000Z","dependencies_parsed_at":"2022-08-24T08:50:52.211Z","dependency_job_id":null,"html_url":"https://github.com/tsingson/fastws-example","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/tsingson/fastws-example","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tsingson%2Ffastws-example","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tsingson%2Ffastws-example/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tsingson%2Ffastws-example/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tsingson%2Ffastws-example/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tsingson","download_url":"https://codeload.github.com/tsingson/fastws-example/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tsingson%2Ffastws-example/sbom","scorecard":{"id":900825,"data":{"date":"2025-08-11","repo":{"name":"github.com/tsingson/fastws-example","commit":"ad5ef3945e28b53f11454a02a29a5d28c197b3d8"},"scorecard":{"version":"v5.2.1-40-gf6ed084d","commit":"f6ed084d17c9236477efd66e5b258b9d4cc7b389"},"score":2.3,"checks":[{"name":"Maintained","score":0,"reason":"0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0","details":null,"documentation":{"short":"Determines if the project is \"actively maintained\".","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#maintained"}},{"name":"Dangerous-Workflow","score":-1,"reason":"no workflows found","details":null,"documentation":{"short":"Determines if the project's GitHub Action workflows avoid dangerous patterns.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#dangerous-workflow"}},{"name":"Packaging","score":-1,"reason":"packaging workflow not detected","details":["Warn: no GitHub/GitLab publishing workflow detected."],"documentation":{"short":"Determines if the project is published as a package that others can easily download, install, easily update, and uninstall.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#packaging"}},{"name":"Code-Review","score":0,"reason":"Found 0/14 approved changesets -- score normalized to 0","details":null,"documentation":{"short":"Determines if the project requires human code review before pull requests (aka merge requests) are merged.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#code-review"}},{"name":"SAST","score":0,"reason":"no SAST tool detected","details":["Warn: no pull requests merged into dev branch"],"documentation":{"short":"Determines if the project uses static code analysis.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#sast"}},{"name":"Binary-Artifacts","score":10,"reason":"no binaries found in the repo","details":null,"documentation":{"short":"Determines if the project has generated executable (binary) artifacts in the source repository.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#binary-artifacts"}},{"name":"Token-Permissions","score":-1,"reason":"No tokens found","details":null,"documentation":{"short":"Determines if the project's workflows follow the principle of least privilege.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#token-permissions"}},{"name":"Pinned-Dependencies","score":-1,"reason":"no dependencies found","details":null,"documentation":{"short":"Determines if the project has declared and pinned the dependencies of its build process.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#pinned-dependencies"}},{"name":"CII-Best-Practices","score":0,"reason":"no effort to earn an OpenSSF best practices badge detected","details":null,"documentation":{"short":"Determines if the project has an OpenSSF (formerly CII) Best Practices Badge.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#cii-best-practices"}},{"name":"Security-Policy","score":0,"reason":"security policy file not detected","details":["Warn: no security policy file detected","Warn: no security file to analyze","Warn: no security file to analyze","Warn: no security file to analyze"],"documentation":{"short":"Determines if the project has published a security policy.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#security-policy"}},{"name":"Fuzzing","score":0,"reason":"project is not fuzzed","details":["Warn: no fuzzer integrations found"],"documentation":{"short":"Determines if the project uses fuzzing.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#fuzzing"}},{"name":"License","score":10,"reason":"license file detected","details":["Info: project has a license file: LICENSE:0","Info: FSF or OSI recognized license: MIT License: LICENSE:0"],"documentation":{"short":"Determines if the project has defined a license.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#license"}},{"name":"Signed-Releases","score":-1,"reason":"no releases found","details":null,"documentation":{"short":"Determines if the project cryptographically signs release artifacts.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#signed-releases"}},{"name":"Branch-Protection","score":0,"reason":"branch protection not enabled on development/release branches","details":["Warn: branch protection not enabled for branch 'master'"],"documentation":{"short":"Determines if the default and release branches are protected with GitHub's branch protection settings.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#branch-protection"}},{"name":"Vulnerabilities","score":4,"reason":"6 existing vulnerabilities detected","details":["Warn: Project is vulnerable to: GO-2022-0355 / GHSA-fx95-883v-4q4h","Warn: Project is vulnerable to: GO-2020-0015 / GHSA-5rcv-m4m3-hfh7","Warn: Project is vulnerable to: GO-2021-0113 / GHSA-ppp9-7jff-5vj2","Warn: Project is vulnerable to: GO-2022-1059 / GHSA-69ch-w2m2-3vjp","Warn: Project is vulnerable to: GHSA-6fc8-4gx4-v693","Warn: Project is vulnerable to: GHSA-3h5v-q93c-6h6q"],"documentation":{"short":"Determines if the project has open, known unfixed vulnerabilities.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#vulnerabilities"}}]},"last_synced_at":"2025-08-24T15:30:30.192Z","repository_id":50780981,"created_at":"2025-08-24T15:30:30.192Z","updated_at":"2025-08-24T15:30:30.192Z"},"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28419272,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-14T10:47:48.104Z","status":"ssl_error","status_checked_at":"2026-01-14T10:46:19.031Z","response_time":107,"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":["fasthttp","fastws","flatbuffers","golang","javascript","router","websocket"],"created_at":"2026-01-14T12:00:39.418Z","updated_at":"2026-01-14T12:00:43.014Z","avatar_url":"https://github.com/tsingson.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n# flatbuffers 在 websocket 中互通的示例\n\n\n## 0. 简要说明\n为某个开源项目增加 websocket 对接, 写了这个示例\n\n代码中 javascript 对 flatbuffers 的序列化/反序列化, 查了一天资料, 嗯哼, 最终完成了.\n看代码吧.........\n\n\n## 1. 使用代码库\n\n示例代码使用了以下开源库\n* [fasthttp](http://github.com/valyala/fasthttp) \n* [fasthttp router](https://github.com/fasthttp/router) \n*  [fastws ](https://github.com/fasthttp/fastws) ----  fasthttp 实现的 websocket 库\n*   [flatbuffers](https://github.com/google/flatbuffers) ---- flatbuffers 高效反序列化通用库, 用在 go语言/javascript \n*   [websockets/ws](https://github.com/websockets/ws) ---- javascript websocket 通用库\n\n## 1. flatbuffers  IDL 示例 \nxone.fbs 示例来自 [https://www.cnblogs.com/sevenstar/p/FlatBuffer.html](https://www.cnblogs.com/sevenstar/p/FlatBuffer.html), 感谢!!\n\n```\nnamespace xone.genflat;\n\n  table LoginRequest{\n  msgID:int=1;\n  username:string;\n  password:string;\n  }\n\n  table LoginResponse{\n msgID:int=2;\n uid:string;\n }\n\n //root_type非必须。\n\n //root_type LoginRequest;\n //root_type LoginRespons\n```\n\n## 2. flatc 编译代码\n\n生成 javascript\n\n```\nflatc -s --gen-mutable ./*.fbs\n```\n\n\n\n生成 golang\n\n```\nflatc  --go --gen-object-api --gen-all  --gen-compare  --raw-binary ./*.fbs\n```\n\n## 3. 主要代码说明\n\n```\n./cmd/wsserver/main.go ----- websocket server \n./cmd/wsclient/main.go ----- websocket client\n./ws/... -------------------  websocket go code for websocket handler and websocket client \n./jsclient/ws.js  ---------- javascript client code , please check-out package.json for depends\n```\n\n\n\n\n## 4. javascript 序列化/反序列化\n\n**请注意代码注释中的--------- 特别注意这一行**\n\n```\n// ------------ ./jsclient/index.js\n\nconst flatbuffers = require('./flatbuffers').flatbuffers;\nconst xone = require('./xone_generated').xone; //Generated by `flatc`.\n\n//-------------------------------------------\n//  serialized\n//-------------------------------------------\nlet b = new flatbuffers.Builder(1);\nlet username = b.createString(\"zlssssssssssssh\");\nlet password = b.createString(\"xxxxxxxxxxxxxxxxxxx\");\nxone.genflat.LoginRequest.startLoginRequest(b);\nxone.genflat.LoginRequest.addUsername(b, username);\nxone.genflat.LoginRequest.addPassword(b, password);\nxone.genflat.LoginRequest.addMsgID(b, 5);\nlet req = xone.genflat.LoginRequest.endLoginRequest(b);\nb.finish(req); //创建结束时记得调用这个finish方法。\n\n\nlet uint8Array = b.asUint8Array();   // ------------- 特别注意这一行\n\nconsole.log(uint8Array);\n// console.log(b.dataBuffer() );\n//-------------------------------------------\n//  un-serialized\n//-------------------------------------------\nlet bb = new flatbuffers.ByteBuffer(uint8Array);  //-------------- 特别注意这一行\nlet lgg = xone.genflat.LoginRequest.getRootAsLoginRequest(bb);\n\n\nconsole.log(\"username: \", lgg.username());\nconsole.log(\"password\", lgg.password());\nconsole.log(\"msgID: \", lgg.msgID());\n\n```\n\n\n\n## 5.  golang 中对 flatbuffers 的序列化/反序列化\n\n```\n\n// ------ ./apis/genflat/model.go\n\nfunc (a *LoginRequestT) Byte() []byte {\n\tb := flatbuffers.NewBuilder(0)\n\tb.Finish(LoginRequestPack(b, a))\n\treturn b.FinishedBytes()\n}\n\nfunc ByteLoginRequestT(b []byte) *LoginRequestT {\n\treturn GetRootAsLoginRequest(b, 0).UnPack()\n}\n\n\n// ------- ./apis/genflat/model_test.go\n\nfunc TestLoginRequestT_Byte(t *testing.T) {\n\tas := assert.New(t)\n\t// serialized\n\tl := \u0026LoginRequestT{\n\t\tMsgID:    1,\n\t\tUsername: \"1\",\n\t\tPassword: \"1\",\n\t}\n\n\tb := l.Byte()\n\n\t// un-serialized \n\tc := ByteLoginRequestT(b)\n\tif l.MsgID \u003e 0 {\n\t\tfmt.Println(\" id \u003e \", c.MsgID, \" u \u003e \", c.Username, \" pw \u003e \", c.Password)\n\t}\n\n\tas.Equal(l.Password, c.Password)\n\n}\n\n```\n\n## 6. websocket 代码\n```\n\nws.onmessage = (event) =\u003e {\n    //-------------------------------------------------------------------\n    //   read from websocket and un-serialized via flatbuffers\n    //--------------------------------------------------------------------\n    let aa = str2ab(event.data);\n    let bb = new flatbuffers.ByteBuffer(aa);\n    let lgg = xone.genflat.LoginRequest.getRootAsLoginRequest(bb);\n    let pw = lgg.password();\n\n    if (typeof pw === 'string') {\n        console.log(\"----------------------------------------------\");\n\n        console.log(\"username: \", lgg.username());\n        console.log(\"password\", lgg.password());\n        console.log(\"msgID: \", lgg.msgID());\n    } else {\n        console.log(\"=================================\");\n        console.log(event.data);\n    }\n\n\n    // console.log(`Roundtrip time: ${Date.now() }` , ab2str(d ));\n\n    setTimeout(function timeout() {\n    //-------------------------------------------------------------------\n    //   serialized via flatbuffers and send to websocket \n    //--------------------------------------------------------------------\n        let b = new flatbuffers.Builder(1);\n        let username = b.createString(\"zlssssssssssssh\");\n        let password = b.createString(\"xxxxxxxxxxxxxxxxxxx\");\n        xone.genflat.LoginRequest.startLoginRequest(b);\n        xone.genflat.LoginRequest.addUsername(b, username);\n        xone.genflat.LoginRequest.addPassword(b, password);\n        xone.genflat.LoginRequest.addMsgID(b, 5);\n        let req = xone.genflat.LoginRequest.endLoginRequest(b);\n        b.finish(req); //创建结束时记得调用这个finish方法。\n\n\n        let uint8Array = b.asUint8Array();\n\n        ws.send(uint8Array);\n    }, 500);\n};\n\nfunction str2ab(str) {\n    let array = new Uint8Array(str.length);\n    for (let i = 0; i \u003c str.length; i++) {\n        array[i] = str.charCodeAt(i);\n    }\n    return array\n}\n\n```\n\n\n\n## 6. 参考\n\n*  [https://github.com/google/flatbuffers/issues/3781](https://github.com/google/flatbuffers/issues/3781)\n\n## 8. License\nMIT\n\n-----\n\ncode  by [tsingson](https://tsingson.github.io)\n\n![tsingson-logo](README.assets/tsingson-logo.png)\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftsingson%2Ffastws-example","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftsingson%2Ffastws-example","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftsingson%2Ffastws-example/lists"}