{"id":771,"date":"2025-04-25T02:16:10","date_gmt":"2025-04-24T18:16:10","guid":{"rendered":"https:\/\/beijian99.top\/?p=771"},"modified":"2025-05-03T02:43:39","modified_gmt":"2025-05-02T18:43:39","slug":"%e5%9f%ba%e4%ba%8ewebsocket%e7%9a%84%e5%b8%a7%e5%90%8c%e6%ad%a5%e8%81%94%e6%9c%bademo%e4%bd%93%e9%aa%8c","status":"publish","type":"post","link":"https:\/\/beijian99.top\/?p=771","title":{"rendered":"\u57fa\u4e8ewebsocket\u7684\u5e27\u540c\u6b65\u8054\u673aDemo\u4f53\u9a8c"},"content":{"rendered":"\n<p>\u9879\u76ee\u5730\u5740\uff1a<a href=\"https:\/\/github.com\/beijian128\/framesync\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/github.com\/beijian128\/framesync<\/a><\/p>\n\n\n\n<p>\u5728\u7ebf\u4f53\u9a8c\uff1a<a href=\"https:\/\/beijian99.top\/framesync\/\" target=\"_blank\" rel=\"noreferrer noopener\">\u70b9\u51fb\u6b64\u5904\u4f20\u9001- \u5e27\u540c\u6b65\u8054\u673aDemo<\/a><\/p>\n\n\n\n<p><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1006\" height=\"571\" src=\"https:\/\/beijian99.top\/wp-content\/uploads\/2025\/04\/image-68.png\" alt=\"\" class=\"wp-image-963\" srcset=\"https:\/\/beijian99.top\/wp-content\/uploads\/2025\/04\/image-68.png 1006w, https:\/\/beijian99.top\/wp-content\/uploads\/2025\/04\/image-68-300x170.png 300w, https:\/\/beijian99.top\/wp-content\/uploads\/2025\/04\/image-68-768x436.png 768w\" sizes=\"auto, (max-width: 1006px) 100vw, 1006px\" \/><\/figure>\n\n\n\n<p>\u670d\u52a1\u7aef<strong>\u6838\u5fc3\u4ee3\u7801 <\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: go; title: ; notranslate\" title=\"\">\n\/\/ FrameSyncInstance \u5e27\u540c\u6b65\u7cfb\u7edf\u7684\u5168\u5c40\u5b9e\u4f8b\nvar FrameSyncInstance = NewFrameSync()\n\n\/\/ FrameSync \u7ba1\u7406\u591a\u4eba\u6e38\u620f\u7684\u5e27\u540c\u6b65\u903b\u8f91\ntype FrameSync struct {\n\t\/\/ curFrame \u5b58\u50a8\u5f53\u524d\u5e27\u6240\u6709\u73a9\u5bb6\u7684\u64cd\u4f5c\u6307\u4ee4\n\t\/\/ key: \u73a9\u5bb6\u8fde\u63a5\u4f1a\u8bdd, value: \u5305\u542b\u8be5\u73a9\u5bb6\u6240\u6709\u64cd\u4f5c\u6307\u4ee4\u7684\u7ed3\u6784\u4f53\n\t\/\/ \u6ce8\u610f\uff1a\u6682\u4e0d\u8003\u8651\u5386\u53f2\u5e27\u5b58\u50a8\uff08\u4e0d\u652f\u6301\u65ad\u7ebf\u91cd\u8fde\u548c\u56de\u653e\u529f\u80fd\uff09\n\tcurFrame map&#x5B;appframe.GateSession]*cmsg.PlayerInput\n\n\t\/\/ ticker \u63a7\u5236\u5e27\u540c\u6b65\u7684\u5b9a\u65f6\u5668\n\tticker *time.Ticker\n\n\t\/\/ userCnt \u73a9\u5bb6ID\u8ba1\u6570\u5668\uff0c\u7528\u4e8e\u5206\u914d\u552f\u4e00ID\n\tuserCnt uint64\n}\n\n\/\/ NewFrameSync \u521b\u5efa\u5e76\u521d\u59cb\u5316\u5e27\u540c\u6b65\u5b9e\u4f8b\nfunc NewFrameSync() *FrameSync {\n\treturn &amp;FrameSync{\n\t\tcurFrame: make(map&#x5B;appframe.GateSession]*cmsg.PlayerInput),\n\t}\n}\n\n\/\/ OnInputCommand \u5904\u7406\u73a9\u5bb6\u8f93\u5165\u6307\u4ee4\n\/\/ session: \u53d1\u9001\u6307\u4ee4\u7684\u73a9\u5bb6\u4f1a\u8bdd\n\/\/ msg: \u5305\u542b\u79fb\u52a8\u65b9\u5411\u7684\u8f93\u5165\u6307\u4ee4\u6d88\u606f\nfunc (fs *FrameSync) OnInputCommand(session appframe.GateSession, msg *cmsg.InputCommand) {\n\t\/\/ \u4ece\u4f1a\u8bdd\u7ba1\u7406\u5668\u83b7\u53d6\u5b8c\u6574\u7684\u4f1a\u8bdd\u5bf9\u8c61\n\ts, _ := SessionMgrInstance.getSession(session.ID())\n\n\t\/\/ \u5982\u679c\u8be5\u73a9\u5bb6\u5728\u5f53\u524d\u5e27\u8fd8\u6ca1\u6709\u8f93\u5165\u8bb0\u5f55\uff0c\u5219\u521d\u59cb\u5316\n\tif fs.curFrame&#x5B;session] == nil {\n\t\tfs.curFrame&#x5B;session] = &amp;cmsg.PlayerInput{\n\t\t\tPlayerId: s.userid,               \/\/ \u73a9\u5bb6ID\n\t\t\tCommands: &#x5B;]*cmsg.InputCommand{}, \/\/ \u6307\u4ee4\u5217\u8868\n\t\t}\n\t}\n\n\t\/\/ \u5c06\u65b0\u6307\u4ee4\u6dfb\u52a0\u5230\u8be5\u73a9\u5bb6\u7684\u5f53\u524d\u5e27\u8f93\u5165\u4e2d\n\tfs.curFrame&#x5B;session].Commands = append(fs.curFrame&#x5B;session].Commands, msg)\n\n\t\/\/ \u79fb\u52a8\u901f\u5ea6\u5e38\u91cf\n\tspeed := int32(3)\n\n\t\/\/ \u6839\u636e\u8f93\u5165\u6307\u4ee4\u66f4\u65b0\u73a9\u5bb6\u4f4d\u7f6e\n\tif msg.Down {\n\t\ts.y += speed \/\/ \u5411\u4e0b\u79fb\u52a8\n\t}\n\tif msg.Up {\n\t\ts.y -= speed \/\/ \u5411\u4e0a\u79fb\u52a8\n\t}\n\tif msg.Left {\n\t\ts.x -= speed \/\/ \u5411\u5de6\u79fb\u52a8\n\t}\n\tif msg.Right {\n\t\ts.x += speed \/\/ \u5411\u53f3\u79fb\u52a8\n\t}\n}\n\n\/\/ OnAddSession \u5904\u7406\u65b0\u73a9\u5bb6\u8fde\u63a5\n\/\/ ss: \u65b0\u52a0\u5165\u7684\u73a9\u5bb6\u4f1a\u8bdd\nfunc (fs *FrameSync) OnAddSession(ss *session) {\n\t\/\/ \u5206\u914d\u552f\u4e00\u73a9\u5bb6ID\n\tfs.userCnt++\n\tss.userid = fs.userCnt\n\n\t\/\/ \u8bbe\u7f6e\u968f\u673a\u521d\u59cb\u4f4d\u7f6e(1-100\u8303\u56f4\u5185)\n\tss.y = rand.Int31n(100) + 1\n\tss.x = rand.Int31n(100) + 1\n\n\t\/\/ \u4e3a\u73a9\u5bb6\u5206\u914d\u968f\u673a\u989c\u8272\n\tss.color = generateRandomColor()\n\n\t\/\/ \u901a\u77e5\u6240\u6709\u73b0\u6709\u73a9\u5bb6\u6709\u65b0\u73a9\u5bb6\u52a0\u5165\n\tSessionMgrInstance.execByEverySession(func(s *session) {\n\t\t\/\/ \u5411\u6bcf\u4e2a\u73b0\u6709\u4f1a\u8bdd\u53d1\u9001\u6240\u6709\u73a9\u5bb6\u4fe1\u606f\uff08\u5305\u62ec\u65b0\u73a9\u5bb6\uff09\n\t\tSessionMgrInstance.execByEverySession(func(s2 *session) {\n\t\t\ts.SendMsg(&amp;cmsg.PlayerEnter{\n\t\t\t\tPlayerId: s2.userid, \/\/ \u73a9\u5bb6ID\n\t\t\t\tX:        s2.x,      \/\/ X\u5750\u6807\n\t\t\t\tY:        s2.y,      \/\/ Y\u5750\u6807\n\t\t\t\tColor:    s2.color,  \/\/ \u73a9\u5bb6\u989c\u8272\n\t\t\t})\n\t\t})\n\t})\n}\n\n\/\/ StartSync \u542f\u52a8\u5e27\u540c\u6b65\u5faa\u73af\n\/\/ ctx: \u4e0a\u4e0b\u6587\u5bf9\u8c61\uff0c\u7528\u4e8e\u63a7\u5236\u534f\u7a0b\u9000\u51fa\nfunc (fs *FrameSync) StartSync(ctx context.Context) {\n\t\/\/ \u521b\u5efa16\u6beb\u79d2\u95f4\u9694\u7684\u5b9a\u65f6\u5668\uff08\u7ea660FPS\uff09\n\tfs.ticker = time.NewTicker(16 * time.Millisecond)\n\tgo func() {\n\t\tfor {\n\t\t\tselect {\n\t\t\tcase &lt;-ctx.Done(): \/\/ \u6536\u5230\u9000\u51fa\u4fe1\u53f7\n\t\t\t\tfs.ticker.Stop()               \/\/ \u505c\u6b62\u5b9a\u65f6\u5668\n\t\t\t\tlogrus.Debug(&quot;FrameSync exit&quot;) \/\/ \u8bb0\u5f55\u8c03\u8bd5\u65e5\u5fd7\n\t\t\t\treturn                         \/\/ \u9000\u51fa\u534f\u7a0b\n\t\t\tcase &lt;-fs.ticker.C: \/\/ \u5b9a\u65f6\u5668\u89e6\u53d1\n\t\t\t\tAppInstance.Post(func() {\n\t\t\t\t\t\/\/ \u5411\u6240\u6709\u4f1a\u8bdd\u5e7f\u64ad\u5f53\u524d\u5e27\u7684\u6240\u6709\u73a9\u5bb6\u8f93\u5165\n\t\t\t\t\tSessionMgrInstance.execByEverySession(func(s *session) {\n\t\t\t\t\t\tfor _, input := range fs.curFrame {\n\t\t\t\t\t\t\ts.SendMsg(input) \/\/ \u53d1\u9001\u73a9\u5bb6\u8f93\u5165\u6570\u636e\n\t\t\t\t\t\t}\n\t\t\t\t\t})\n\t\t\t\t\tclear(fs.curFrame) \/\/ \u6e05\u7a7a\u5f53\u524d\u5e27\u6570\u636e\n\t\t\t\t})\n\t\t\t}\n\t\t}\n\t}()\n}\n\n\/\/ RemoveSession \u5904\u7406\u73a9\u5bb6\u9000\u51fa\n\/\/ sess: \u8981\u79fb\u9664\u7684\u73a9\u5bb6\u4f1a\u8bdd\nfunc (fs *FrameSync) RemoveSession(sess *session) {\n\tdelete(fs.curFrame, sess.GateSession) \/\/ \u4ece\u5f53\u524d\u5e27\u79fb\u9664\u8be5\u73a9\u5bb6\n\n\t\/\/ \u901a\u77e5\u5176\u4ed6\u73a9\u5bb6\u8be5\u73a9\u5bb6\u5df2\u9000\u51fa\n\tSessionMgrInstance.execByEverySession(func(s *session) {\n\t\tif s.ID() == sess.ID() { \/\/ \u4e0d\u901a\u77e5\u81ea\u5df1\n\t\t\treturn\n\t\t}\n\t\ts.SendMsg(&amp;cmsg.PlayerLeave{PlayerId: sess.userid}) \/\/ \u53d1\u9001\u73a9\u5bb6\u79bb\u5f00\u6d88\u606f\n\t})\n}\n<\/pre><\/div>\n\n\n<p><strong>C\/S \u4ea4\u4e92 \u6d88\u606f\u5b9a\u4e49\uff1a<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: go; title: ; notranslate\" title=\"\">\n\/\/ ================== \u8f93\u5165\u6307\u4ee4 ==================\nmessage InputCommand {\n  bool up = 1;            \/\/ W\u952e\n  bool left = 2;          \/\/ A\u952e\n  bool down = 3;          \/\/ S\u952e\n  bool right = 4;         \/\/ D\u952e\n}\n\n\/\/ ================== \u73a9\u5bb6\u6307\u4ee4\u96c6 ==================\nmessage PlayerInput {\n  uint64 playerId = 1;\n  repeated InputCommand commands = 2; \/\/ \u8be5\u73a9\u5bb6\u7684\u6240\u6709\u6307\u4ee4\n}\n\n\/\/ \u65b0\u73a9\u5bb6\u521a\u8fdb\u5165\uff0c\u540c\u6b65\u4e00\u6b21\u72b6\u6001\nmessage PlayerEnter{\n  uint64 playerId = 1;\n  int32 x = 2;\n  int32 y = 3;\n  string color = 4;\n}\n\n\/\/ \u67d0\u4e2a\u73a9\u5bb6\u79bb\u5f00\nmessage PlayerLeave{\n  uint64 playerId = 1;\n}\n<\/pre><\/div>\n\n\n<p><\/p>\n\n\n\n<p>\u5ba2\u6237\u7aef\u4ee3\u7801\u548c\u66f4\u591a\u5b8c\u6574\u4ee3\u7801\u8be6\u89c1<a href=\"https:\/\/github.com\/beijian128\/framesync\">  https:\/\/github.com\/beijian128\/framesync<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9879\u76ee\u5730\u5740\uff1ahttps:\/\/github.com\/beijian128\/framesync \u5728\u7ebf\u4f53\u9a8c\uff1a\u70b9\u51fb\u6b64\u5904\u4f20 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":963,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[121,131],"tags":[132],"class_list":["post-771","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-golang","category-131","tag-132"],"_links":{"self":[{"href":"https:\/\/beijian99.top\/index.php?rest_route=\/wp\/v2\/posts\/771","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/beijian99.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/beijian99.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/beijian99.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/beijian99.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=771"}],"version-history":[{"count":6,"href":"https:\/\/beijian99.top\/index.php?rest_route=\/wp\/v2\/posts\/771\/revisions"}],"predecessor-version":[{"id":964,"href":"https:\/\/beijian99.top\/index.php?rest_route=\/wp\/v2\/posts\/771\/revisions\/964"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/beijian99.top\/index.php?rest_route=\/wp\/v2\/media\/963"}],"wp:attachment":[{"href":"https:\/\/beijian99.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=771"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/beijian99.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=771"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/beijian99.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=771"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}