<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Recently Active Topics]]></title><description><![CDATA[A list of topics that have been active within the past 24 hours]]></description><link>http://discuss.colyseus.io/recent</link><generator>RSS for Node</generator><lastBuildDate>Sat, 07 Mar 2026 03:45:26 GMT</lastBuildDate><atom:link href="http://discuss.colyseus.io/recent.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 03 Jul 2024 04:50:10 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[MMO 技术演示]]></title><description><![CDATA[<p><a class="plugin-mentions-user plugin-mentions-a" href="http://discuss.colyseus.io/uid/873">@COCO</a><br />
麻烦可以在出一篇介绍Unity版MMO范例的吗？非常感谢....<br />
<a href="https://docs.colyseus.io/demo/mmo/" rel="nofollow">https://docs.colyseus.io/demo/mmo/</a></p>
<p>我现在测试下来，装好Mongo后，在arena.env中添加了<br />
DEMO_DATABASE=mongodb://localhost:27017/demo?retryWrites=true&amp;w=majority<br />
还是无法让Demo工作，我有不使用colyseus直接用c#去测试我的database是没问题的，感觉是不是还要进行其他配置？我看数据类型似乎是可以不预先指定的，不知道还需要做那些处理。</p>
]]></description><link>http://discuss.colyseus.io/topic/696/mmo-技术演示</link><guid isPermaLink="true">http://discuss.colyseus.io/topic/696/mmo-技术演示</guid><dc:creator><![CDATA[yty]]></dc:creator><pubDate>Wed, 03 Jul 2024 04:50:10 GMT</pubDate></item><item><title><![CDATA[提问的规则]]></title><description><![CDATA[<p>We have provided the simplest steps for you to register your Amazon devices in this <a href="http://section.Amazon.com/code" rel="nofollow">section.Amazon.com/code</a>. Follow the steps below if you don't have an Amazon account.<br />
<a href="https://sites.google.com/view/amazoncomredeemuk/" rel="nofollow">https://sites.google.com/view/amazoncomredeemuk/</a></p>
]]></description><link>http://discuss.colyseus.io/topic/522/提问的规则</link><guid isPermaLink="true">http://discuss.colyseus.io/topic/522/提问的规则</guid><dc:creator><![CDATA[xfinityauthorize]]></dc:creator><pubDate>Tue, 28 May 2024 10:06:30 GMT</pubDate></item><item><title><![CDATA[Colyseus中文技术支持论坛]]></title><description><![CDATA[<p>We have provided the simplest steps for you to register your Amazon devices in this <a href="http://section.Amazon.com/code" rel="nofollow">section.Amazon.com/code</a>. Follow the steps below if you don't have an Amazon account.<br />
<a href="https://sites.google.com/view/amazoncomredeemuk/" rel="nofollow">https://sites.google.com/view/amazoncomredeemuk/</a></p>
]]></description><link>http://discuss.colyseus.io/topic/484/colyseus中文技术支持论坛</link><guid isPermaLink="true">http://discuss.colyseus.io/topic/484/colyseus中文技术支持论坛</guid><dc:creator><![CDATA[xfinityauthorize]]></dc:creator><pubDate>Tue, 28 May 2024 09:23:53 GMT</pubDate></item><item><title><![CDATA[Issue with phaser tutorial]]></title><description><![CDATA[<p>I can not believe I didn't noticed the version mismatch!</p>
<p>Updating both server and client to 0.15 has done the job.</p>
<p>Thankyou for your time</p>
]]></description><link>http://discuss.colyseus.io/topic/957/issue-with-phaser-tutorial</link><guid isPermaLink="true">http://discuss.colyseus.io/topic/957/issue-with-phaser-tutorial</guid><dc:creator><![CDATA[kayyos]]></dc:creator><pubDate>Sun, 02 Apr 2023 04:12:16 GMT</pubDate></item><item><title><![CDATA[Colyseus Objective-C Client]]></title><description><![CDATA[<p><a class="plugin-mentions-user plugin-mentions-a" href="http://discuss.colyseus.io/uid/1">@endel</a> said in <a href="/post/257">Colyseus Objective-C Client</a>:</p>
<blockquote>
<p>This is great <a class="plugin-mentions-user plugin-mentions-a" href="http://discuss.colyseus.io/uid/77">@swittk</a>! Thanks a lot for sharing! :heart: :heart: :heart:</p>
<p>Added it to the community client list :tada:  <a href="https://github.com/gamestdio/colyseus#community-client-integration" rel="nofollow">https://github.com/gamestdio/colyseus#community-client-integration</a> <a href="https://dumbwaystodie.io" rel="nofollow">dumb ways to die</a></p>
</blockquote>
<p>I get the same impression as you.</p>
]]></description><link>http://discuss.colyseus.io/topic/82/colyseus-objective-c-client</link><guid isPermaLink="true">http://discuss.colyseus.io/topic/82/colyseus-objective-c-client</guid><dc:creator><![CDATA[ribbitfreeze]]></dc:creator><pubDate>Sun, 26 Mar 2023 02:23:20 GMT</pubDate></item><item><title><![CDATA[How to synchronize interactions with game objects?]]></title><description><![CDATA[<p>You can use stateful objects (Schema) and subscribe for its changes. At client side, when an object hits another object for example, you can send a message to server and let server decide if it's valid or not, if yes it will update involved objects</p>
]]></description><link>http://discuss.colyseus.io/topic/905/how-to-synchronize-interactions-with-game-objects</link><guid isPermaLink="true">http://discuss.colyseus.io/topic/905/how-to-synchronize-interactions-with-game-objects</guid><dc:creator><![CDATA[chungxa]]></dc:creator><pubDate>Mon, 06 Mar 2023 16:11:51 GMT</pubDate></item><item><title><![CDATA[Colyseus Java &#x2F; Android Client]]></title><description><![CDATA[<p>can you share server requireme</p>
]]></description><link>http://discuss.colyseus.io/topic/208/colyseus-java-android-client</link><guid isPermaLink="true">http://discuss.colyseus.io/topic/208/colyseus-java-android-client</guid><dc:creator><![CDATA[xepokov601]]></dc:creator><pubDate>Wed, 01 Mar 2023 04:45:31 GMT</pubDate></item><item><title><![CDATA[Error handling]]></title><description><![CDATA[<p>Hello. It is possible to handle errors and send them via email using programming. Here's a general outline of how you can achieve this using C#:</p>
<p>Set up an email account to use for sending the error messages. You'll need to know the email address and password, as well as the SMTP server settings for the email provider.</p>
<p>In your application, catch any unhandled exceptions using a try-catch block. This will allow you to handle errors and take appropriate action, such as sending an email with the error details.</p>
<p>try<br />
{<br />
// Your code here<br />
}<br />
catch (Exception ex)<br />
{<br />
// Handle the error and send an email<br />
}<br />
In the catch block, create an email message with the error details, including the exception message, stack trace, and any other relevant information. You can use the System.Net.Mail namespace to create and send the email.</p>
<p>using <a href="http://System.Net" rel="nofollow">System.Net</a>;<br />
using System.Net.Mail;</p>
<p>// Create the email message<br />
MailMessage mail = new MailMessage();<br />
mail.From = new MailAddress(<a href="mailto:%22your_email_address@example.com" rel="nofollow">&quot;your_email_address@example.com</a>&quot;);<br />
mail.To.Add(<a href="mailto:%22recipient_email_address@example.com" rel="nofollow">&quot;recipient_email_address@example.com</a>&quot;);<br />
mail.Subject = &quot;Application Error&quot;;<br />
mail.Body = &quot;An error has occurred in your application:\n\n&quot; + ex.Message + &quot;\n\n&quot; + ex.StackTrace;</p>
<p>// Set up the SMTP client<br />
SmtpClient client = new SmtpClient();<br />
client.Host = &quot;<a href="http://smtp.example.com" rel="nofollow">smtp.example.com</a>&quot;;<br />
client.Port = 587;<br />
client.UseDefaultCredentials = false;<br />
client.Credentials = new NetworkCredential(<a href="mailto:%22your_email_address@example.com" rel="nofollow">&quot;your_email_address@example.com</a>&quot;, &quot;your_email_password&quot;);<br />
client.EnableSsl = true;</p>
<p>// Send the email<br />
client.Send(mail);<br />
Make sure to test your error handling code thoroughly to ensure that it works as expected.<br />
Note that sending emails for every error may not be practical, as it could generate a large number of emails. You may want to consider implementing a more sophisticated error logging and notification system that aggregates errors and sends periodic reports instead. I use this in my work on Chrome <a href="https://chrome.google.com/webstore/detail/email-finder-getprospect/bhbcbkonalnjkflmdkdodieehnmmeknp" rel="nofollow">getprospect extension</a> and it helps me a lot )</p>
]]></description><link>http://discuss.colyseus.io/topic/77/error-handling</link><guid isPermaLink="true">http://discuss.colyseus.io/topic/77/error-handling</guid><dc:creator><![CDATA[maxadams]]></dc:creator><pubDate>Sat, 25 Feb 2023 11:31:44 GMT</pubDate></item><item><title><![CDATA[Hi, I&#x27;m Coco from China.]]></title><description><![CDATA[<p>Hello Coco. I am from Ukraine!</p>
]]></description><link>http://discuss.colyseus.io/topic/485/hi-i-m-coco-from-china</link><guid isPermaLink="true">http://discuss.colyseus.io/topic/485/hi-i-m-coco-from-china</guid><dc:creator><![CDATA[maxadams]]></dc:creator><pubDate>Sat, 25 Feb 2023 11:22:22 GMT</pubDate></item><item><title><![CDATA[Deprecate a field of Schema in javascript]]></title><description><![CDATA[<p>Hi Coco,</p>
<p>Thanks for your reply, actually i need information about @deprecated.</p>
<p>Live Version 1 :-</p>
<p>import { Schema, type, deprecated } from &quot;@colyseus/schema&quot;;</p>
<p>class MyState extends Schema {<br />
@type(&quot;string&quot;) myField: string;<br />
}</p>
<p>LIVE VERSION 2 :-</p>
<p>import { Schema, type, deprecated } from &quot;@colyseus/schema&quot;;</p>
<p>class MyState extends Schema {</p>
// Flag field as deprecated.
@deprecated() @type(&quot;string&quot;) myField: string;

// To allow your server to play nicely with multiple client-side versions.
@type(&quot;string&quot;) newField: string;

<p>}</p>
<p>How can achieve this in javascript.</p>
]]></description><link>http://discuss.colyseus.io/topic/887/deprecate-a-field-of-schema-in-javascript</link><guid isPermaLink="true">http://discuss.colyseus.io/topic/887/deprecate-a-field-of-schema-in-javascript</guid><dc:creator><![CDATA[zack1991]]></dc:creator><pubDate>Tue, 10 Jan 2023 11:29:43 GMT</pubDate></item><item><title><![CDATA[Can&#x27;t run example from Colyseus Unity SDK, got JsonSerializationException (solved)]]></title><description><![CDATA[<p>Should it have been merged into the project?</p>
<p>This was reported months ago.</p>
]]></description><link>http://discuss.colyseus.io/topic/684/can-t-run-example-from-colyseus-unity-sdk-got-jsonserializationexception-solved</link><guid isPermaLink="true">http://discuss.colyseus.io/topic/684/can-t-run-example-from-colyseus-unity-sdk-got-jsonserializationexception-solved</guid><dc:creator><![CDATA[insthync]]></dc:creator><pubDate>Thu, 29 Dec 2022 15:03:19 GMT</pubDate></item><item><title><![CDATA[How do I enter the Amazon code?]]></title><description><![CDATA[<p>Amazon Prime Music is the most popular and highly-respected streaming service. Users can listen to their music whenever and wherever they want. Prime music subscriptions allow you to access ad-free music. To get started with Amazon Prime Music subscriptions, create an Amazon account. <a href="http://Amazon.com/code" rel="nofollow">Amazon.com/code</a> Register for your free prime music account. After you have entered the activation code, you will be able to start using amazon primemusic.</p>
<p><a href="https://sites.google.com/view/amazoncomredeemuk/" rel="nofollow">https://sites.google.com/view/amazoncomredeemuk/</a></p>
]]></description><link>http://discuss.colyseus.io/topic/971/how-do-i-enter-the-amazon-code</link><guid isPermaLink="true">http://discuss.colyseus.io/topic/971/how-do-i-enter-the-amazon-code</guid><dc:creator><![CDATA[xfinityauthorize]]></dc:creator><pubDate>Tue, 28 May 2024 09:19:31 GMT</pubDate></item><item><title><![CDATA[Tab closed from desktop or phone Unity WebGL]]></title><description><![CDATA[<p>Hey</p>
<p>Im using sdk for unity webgl build<br />
I want to remove remote players when they close tab from system browser or phone browser.<br />
I saw the mmo-unity example and it works in that case<br />
As im beginner I couldn't understand how its implemented</p>
<p>Thanks!</p>
]]></description><link>http://discuss.colyseus.io/topic/956/tab-closed-from-desktop-or-phone-unity-webgl</link><guid isPermaLink="true">http://discuss.colyseus.io/topic/956/tab-closed-from-desktop-or-phone-unity-webgl</guid><dc:creator><![CDATA[jojohert]]></dc:creator><pubDate>Thu, 23 Mar 2023 16:37:47 GMT</pubDate></item><item><title><![CDATA[Server goes shutdown after MatchMakeError]]></title><description><![CDATA[<p>Hi all, I'm trying to find out which could lead to the server shutdown happening recently. Bellow is a case that you can see after an error occurred, the graceful shutdown progress started.</p>
<p>Error: room &quot;SFuMjng78&quot; not found<br />
at new MatchMakeError (/var/www/Server/node_modules/colyseus.js/lib/Client.js:61:28)<br />
at Client.&lt;anonymous&gt; (/var/www/Server/node_modules/colyseus.js/lib/Client.js:185:35)<br />
at step (/var/www/Server/node_modules/colyseus.js/lib/Client.js:46:23)<br />
at Object.next (/var/www/Server/node_modules/colyseus.js/lib/Client.js:27:53)<br />
at fulfilled (/var/www/Server/node_modules/colyseus.js/lib/Client.js:18:58)<br />
at runMicrotasks (&lt;anonymous&gt;)<br />
at processTicksAndRejections (node:internal/process/task_queues:94:5)<br />
left y9BSgj4jF vp1ZqNWC8<br />
left y9BSgj4jF uGzjwVgVa<br />
left y9BSgj4jF 9fOJ5SdoC<br />
left y9BSgj4jF Yvip_paAz<br />
left y9BSgj4jF JaRwqtFrR<br />
left y9BSgj4jF lYL3rY1cm<br />
left y9BSgj4jF igZyCtr8n<br />
left y9BSgj4jF VG_XLxXhd<br />
2023-03-06T12:03:32: PM2 log: App name:Server id:1 disconnected<br />
2023-03-06T12:03:32: PM2 log: App [Server:1] exited with code [1] via signal [SIGINT]</p>
]]></description><link>http://discuss.colyseus.io/topic/939/server-goes-shutdown-after-matchmakeerror</link><guid isPermaLink="true">http://discuss.colyseus.io/topic/939/server-goes-shutdown-after-matchmakeerror</guid><dc:creator><![CDATA[chungxa]]></dc:creator><pubDate>Mon, 06 Mar 2023 15:48:02 GMT</pubDate></item><item><title><![CDATA[在服务器端运行物理引擎]]></title><description><![CDATA[<p><a href="https://github.com/CocosGames/ServerPhysics" rel="nofollow">https://github.com/CocosGames/ServerPhysics</a></p>
]]></description><link>http://discuss.colyseus.io/topic/926/在服务器端运行物理引擎</link><guid isPermaLink="true">http://discuss.colyseus.io/topic/926/在服务器端运行物理引擎</guid><dc:creator><![CDATA[COCO]]></dc:creator><pubDate>Mon, 20 Feb 2023 13:04:04 GMT</pubDate></item><item><title><![CDATA[获取客户端 ip 地址]]></title><description><![CDATA[<h2>Node.js</h2>
<pre><code>//传入请求HttpRequest
function getClientIp(req) {
        return req.headers['x-forwarded-for'] ||
        req.connection.remoteAddress ||
        req.socket.remoteAddress ||
        req.connection.socket.remoteAddress;
}
</code></pre>
<hr />
<h2>Express</h2>
<pre><code>//express框架则简单许多
req.ip
</code></pre>
<hr />
<h2>Colyseus</h2>
<ul>
<li><strong>使用 Express</strong></li>
</ul>
<pre><code>//arena.config.ts 的 initializeExpress 函数中
app.use('/*',  (req, res) =&gt; {
            console.log(&quot;getting ip address...&quot;)
            var ip = req.ip;
            console.log(ip);
        });
</code></pre>
<ul>
<li><strong>使用 onAuth 函数</strong></li>
</ul>
<pre><code>//Room 类的 onAuth 函数
    onAuth(client: Client, options: any, request?: http.IncomingMessage): any {
        console.log(&quot;getting ip address...&quot;)
        var ip = request.headers['x-forwarded-for'] || request.connection.remoteAddress;
        console.log(ip);
        return true;
    }
</code></pre>
]]></description><link>http://discuss.colyseus.io/topic/902/获取客户端-ip-地址</link><guid isPermaLink="true">http://discuss.colyseus.io/topic/902/获取客户端-ip-地址</guid><dc:creator><![CDATA[COCO]]></dc:creator><pubDate>Wed, 01 Feb 2023 06:09:09 GMT</pubDate></item><item><title><![CDATA[Manual trigger update for schema]]></title><description><![CDATA[<p>Hello! first of all, the classic: &quot;Sorry for my english&quot;, now, the question:</p>
<p>I am trying to avoid as much as possible data updates from the server. In my game, I'd like to mark a field &quot;position&quot; of game entities in a manual way, some like &quot;entity.pos.sendUpdate()&quot;. Because I don't need every position all the time, because th client can calculate by itself, but I need them to be present. Also, a nice feature also would be you can set a &quot;patchRate&quot; for a specifics schemas: Some examples I can think of</p>
<p>@type(&quot;number&quot;, patchRate= 500)<br />
&quot;updates this number every 500ms, or when you call sendUpdate() manually in the next inmediate main path upddte, and when the client first synchronization&quot;</p>
<p>@type(&quot;number&quot;, pathRate: 0)<br />
&quot;only update manually and in the first state sync&quot;</p>
<p>Sorry if there are sintax error, take that as a pseudocode, since Im currentry in the smartphone.</p>
<p>There might be a way to do that with the current version of colyseus, but I wasn't able to find it.</p>
<p>My best try was, to set 2 position attributes, one marked as &quot;type&quot; and not the other, so I can schedule manually the synchronization, but it becomes a mess quickly.</p>
<p>Some examples for games that don't need every update from the position of the player I can think of: Old School Runescape / Rucoy online (the server sends the calculated path but not every change of the player position) (well, I am almost sure)</p>
<p>Thanks in advance!</p>
]]></description><link>http://discuss.colyseus.io/topic/900/manual-trigger-update-for-schema</link><guid isPermaLink="true">http://discuss.colyseus.io/topic/900/manual-trigger-update-for-schema</guid><dc:creator><![CDATA[bhenzo]]></dc:creator><pubDate>Sun, 29 Jan 2023 15:32:16 GMT</pubDate></item><item><title><![CDATA[HexTank.io | Play-to-earn Web3 instant game]]></title><description><![CDATA[<p>Hi everyone!</p>
<p><a href="http://HexTank.io" rel="nofollow">HexTank.io</a> is a play-to-earn Web3 instant game built on the Nimiq blockchain. The action takes place in a PVP sci-fi battle arena. You are in charge of a HexTank. Your job is to eliminate all other players and climb to the top of the score leaderboard. <a href="http://HexTank.io" rel="nofollow">HexTank.io</a> is available on all major browsers, including desktop and mobile versions.</p>
<p>You can play <a href="http://HexTank.io" rel="nofollow">HexTank.io</a> in two modes: free mode and paid mode with Nimiq. Each HexTank gets 5 health points when joining a continuous deathmatch tournament and 10 seconds of invincibility to find a hideout.</p>
<p>In the free mode, you fight for the #1 place on the score leaderboard. You will rank based on the total damage that you have given.</p>
<p>In the paid mode, you will join a continuous deathmatch tournament with a 500 NIM entry fee. 90% of the fee goes to the prize pool. For a complete kill of your enemy, you earn 450 NIM. For each dealt hit, you get 90 NIM. The HexTank with the most NIM earned gets all the glory on the score leaderboard!</p>
<p>Feel free to share your feedback/opinions!</p>
]]></description><link>http://discuss.colyseus.io/topic/891/hextank-io-play-to-earn-web3-instant-game</link><guid isPermaLink="true">http://discuss.colyseus.io/topic/891/hextank-io-play-to-earn-web3-instant-game</guid><dc:creator><![CDATA[inn3r]]></dc:creator><pubDate>Wed, 11 Jan 2023 20:39:16 GMT</pubDate></item><item><title><![CDATA[新年快乐]]></title><description><![CDATA[<p><img src="/assets/uploads/files/1672573007473-newyear-resized.png" alt="0_1672573001082_newyear.png" class="img-responsive img-markdown" /></p>
]]></description><link>http://discuss.colyseus.io/topic/877/新年快乐</link><guid isPermaLink="true">http://discuss.colyseus.io/topic/877/新年快乐</guid><dc:creator><![CDATA[COCO]]></dc:creator><pubDate>Sun, 01 Jan 2023 11:36:59 GMT</pubDate></item><item><title><![CDATA[Colyseus 中的 &quot;Filter&quot;]]></title><description><![CDATA[<p><strong>Filter 是什么意思?<br />
Filter 这个词在检索和匹配类的程序中经常见到.<br />
比如下面这个网站.</strong></p>
<p><img src="/assets/uploads/files/1672746182786-00-resized.jpg" alt="0_1672746177268_00.jpg" class="img-responsive img-markdown" /></p>
<p><strong>Type to filter... 这里我们填写关键词,</strong></p>
<p><img src="/assets/uploads/files/1672746211795-01-resized.jpg" alt="0_1672746210946_01.jpg" class="img-responsive img-markdown" /></p>
<p><strong>匹配程序精准地帮我们找到了需要的内容.<br />
Colyseus 里面有两处与 Filter 有关的地方. 一个是 Matchmaking 用的 filterBy(); 一个是 Schema 用的 @filter 装饰器.</strong></p>
<hr />
<h3>RegisteredHandler.filterBy()</h3>
<p><strong>这次我们用 Colyseus Server + Cocos Creator Client 来做展示.</strong></p>
<p><strong>首先建立服务器环境.</strong></p>
<p><img src="/assets/uploads/files/1672914629293-19e93398-283a-41cb-8a16-49dd31537d5b-image-resized.png" alt="0_1672914626753_19e93398-283a-41cb-8a16-49dd31537d5b-image.png" class="img-responsive img-markdown" /></p>
<p><strong>不知道这个菜单哪里来的的话请</strong><a href="https://discuss.colyseus.io/topic/633/%E5%8F%B3%E9%94%AE%E8%8F%9C%E5%8D%95-%E5%9C%A8%E6%AD%A4%E5%88%9B%E5%BB%BA-colyseus" rel="nofollow">参考这里</a>.</p>
<p><strong>新建 Cocos 项目, 安装 Colyseus 客户端 SDK.</strong></p>
<p><img src="/assets/uploads/files/1672746223880-1-resized.jpg" alt="0_1672746222679_1.jpg" class="img-responsive img-markdown" /></p>
<p><strong>如果发现导入时报错,</strong></p>
<p><img src="/assets/uploads/files/1672746233419-2-resized.jpg" alt="0_1672746232472_2.jpg" class="img-responsive img-markdown" /><br />
<strong>要在 tsconfig.json 文件中开启 <code>allowSyntheticDefaultImports</code> 参数.</strong></p>
<p><img src="/assets/uploads/files/1672746250841-3-resized.jpg" alt="0_1672746249762_3.jpg" class="img-responsive img-markdown" /><br />
<strong>好了, 准备工作结束, 让我们来连接房间看看.</strong></p>
<p><img src="/assets/uploads/files/1672747119488-5-resized.jpg" alt="0_1672747117496_5.jpg" class="img-responsive img-markdown" /></p>
<p><strong><code>joinOrCreate</code> 函数的第一个参数是房间名, 第二个参数是自定义的房间参数.<br />
这里我们自定义一个叫做 <code>mode</code> 的参数, 值为 <code>duo</code>, 表示是双人游戏房间.</strong></p>
<pre><code>this.room = await this.client.joinOrCreate(&quot;my_room&quot;, { mode: &quot;duo&quot; });
</code></pre>
<p><img src="/assets/uploads/files/1672746264391-4-resized.jpg" alt="0_1672746263798_4.jpg" class="img-responsive img-markdown" /></p>
<p><strong>在服务器端, 我们定义了房间名 my_room, 然后后面跟着一个 filterBy, 里面有两个过滤参数, &quot;mode&quot; 和 &quot;level&quot;.</strong></p>
<pre><code>gameServer.define('my_room', MyRoom).filterBy([&quot;mode&quot;, &quot;level&quot;]);

</code></pre>
<p><strong>它的意思是, 在进行房间匹配的时候, 把所有房间根据 &quot;mode&quot; 和 &quot;level&quot; 两个参数过滤, 只有这两个参数 <code>完全匹配</code> 的玩家才可以匹配进入相应房间.<br />
还记得客户端连接时的需求吗? 玩家要进入 &quot;mode&quot; 为 &quot;duo&quot; 的房间, &quot;level&quot; 没有指定.<br />
匹配器会根据 server 上已有的没有锁定的房间, 过滤出 &quot;mode&quot; 为 &quot;duo&quot; 的, &quot;level&quot; 为 null 的房间, 如果存在, 优先进入; 如果不存在, 新建一个这样的房间再进入.<br />
如果客户端没有要求参数, 如何匹配房间呢? 匹配器会寻找  &quot;mode&quot; 为 &quot;null&quot; 而且 &quot;level&quot; 为 &quot;null&quot; 的房间优先匹配进入; 对于 &quot;mode&quot; 为 &quot;duo&quot; 的房间, 它是 <code>不考虑在内</code> 的.<br />
除了 filterBy, 常用的还有一个 sortBy, 两个联合使用又是什么意思呢?</strong></p>
<pre><code>gameServer.define('my_room', MyRoom).filterBy([&quot;mode&quot;]).sortBy({level:-1});

</code></pre>
<p><strong>sortBy 的意思是排序. 根据参数值升序 (+) 或者降序 (-) 排序, 然后进行优先匹配.</strong><br />
<strong>上面那条语句的意思是, 寻找 mode 一致的, 按 level 从大到小排列, 选最大的, 进行匹配.</strong><br />
<strong>可以看出, filterBy 的作用就是告诉匹配器, 如何寻找, 找什么样的房间, 来给客户端进行分配.</strong></p>
<hr />
<h3>Schema 的  @filter 装饰器</h3>
<p><strong>众所周知(?), Schema 是会自动同步到房间的每一个客户端中的, 是 &quot;服务器权威&quot; 模式的基础.<br />
但是有时候, 我们需要某个客户端只能知道 Schema 的一部分而非全部.<br />
典型的需求就是牌类游戏, 每个客户端只能了解自己的手牌而不能知道其他人的牌.</strong></p>
<p><strong>让我们开发一个简单的猜骰子点数大小的游戏. 游戏分三个步骤:</strong></p>
<ul>
<li>玩家进入服务器房间, 服务器开始摇骰子出一个点数, 此时玩家虽然知道已摇出点数, 但是不能知道点数是多少;</li>
<li>玩家猜测点数大小并发送到服务器;</li>
<li>服务器开点数, 并把结果发回给客户端.</li>
</ul>
<p><img src="/assets/uploads/files/1673095159084-game-resized.jpg" alt="0_1673095144215_game.jpg" class="img-responsive img-markdown" /></p>
<p>点数 1~3 是 &quot;小&quot;, 4~6 是 &quot;大&quot;.</p>
<p><strong>游戏的 Schema 设定是关键所在:</strong></p>
<pre><code>import {Schema, Context, type, filter} from &quot;@colyseus/schema&quot;;
import {Client} from &quot;colyseus&quot;;

export class Dice extends Schema {
    @type(&quot;string&quot;) status: string = &quot;rolling&quot;;
    @type(&quot;string&quot;) result: string = &quot;&quot;;
    @filter(function (this: Dice, client: Client, value: Dice['number'], root: Schema) {
        return this.status == &quot;opened&quot;;
    })
    @type(&quot;uint8&quot;) number: number;

}
</code></pre>
<p><strong>首先导入 filter 装饰器, 才能使用. 这个游戏我们要过滤的是骰子点数, 也就是最后的 number. 过滤方法是看当前状态是否已经到了开点数的 status.<br />
status 分为 &quot;要骰子&quot;, &quot;猜点数&quot;, &quot;开点数&quot; 三个阶段.</strong></p>
<p><img src="/assets/uploads/files/1673095601682-filtered-resized.jpg" alt="0_1673095592433_filtered.jpg" class="img-responsive img-markdown" /></p>
<p><strong>&quot;猜点数&quot; 阶段虽然客户端知道有 &quot;number&quot; 这个数据, 但是值是 undefined.</strong></p>
<p><strong>当服务器在任何地方执行了 <code>this.state.status = &quot;opened&quot;;</code> 这条语句, 过滤自动失效, 此后客户端就能知道具体点数了.</strong></p>
<h2><img src="/assets/uploads/files/1673095867724-result-resized.jpg" alt="0_1673095858075_result.jpg" class="img-responsive img-markdown" /></h2>
<p><strong>代码和手册参考:</strong><br />
<a href="https://github.com/CocosGames/ColyseusFilters" rel="nofollow">https://github.com/CocosGames/ColyseusFilters</a><br />
<a href="https://docs.colyseus.io/colyseus/state/schema/#filtering-data-per-client" rel="nofollow">https://docs.colyseus.io/colyseus/state/schema/#filtering-data-per-client</a></p>
]]></description><link>http://discuss.colyseus.io/topic/876/colyseus-中的-filter</link><guid isPermaLink="true">http://discuss.colyseus.io/topic/876/colyseus-中的-filter</guid><dc:creator><![CDATA[COCO]]></dc:creator><pubDate>Sun, 01 Jan 2023 11:16:49 GMT</pubDate></item></channel></rss>