简体   繁体   English

使用Meteor将数据从服务器流式传输到客户端:

[英]Streaming data from the Server to Client with Meteor:

I'm developing a text based adventure game with Meteor and I'm running into an issue with how to handle certain elements. 我正在使用Meteor开发基于文本的冒险游戏,我遇到了如何处理某些元素的问题。 Namely, how to emit data from the Server to the Client without any input from the Client. 即,如何在没有客户端任何输入的情况下将数据从服务器发送到客户端。

The idea is that when a player is engaged in combat with a monster, the combat damage and updating the Player and Monster objects will be occurring in a loop on the server. 这个想法是,当玩家与怪物进行战斗时,战斗损坏和更新玩家和怪物对象将在服务器上循环发生。 When the player takes damage it should accordingly update the client UI. 当玩家受到伤害时,它应相应地更新客户端UI。 Is something like this possible with Publish / Subscribe? 发布/订阅是否可以这样?

I basically want something that sits and listens for events from the server to update the combat log accordingly. 我基本上想要一些东西,并且从服务器监听事件以相应地更新战斗日志。

In pseudo-code, this is something along the lines of what I'm looking for: 在伪代码中,这是我正在寻找的东西:

// Client Side:
Meteor.call("runCommand", "attack monster");

// Server Side
Meteor.methods({
    runCommand: function(input) {
        // Take input, run the loop to begin combat, 
        // whenever the user takes damage update the 
        // client UI and output a line saying how much 
        // damage the player just received and by who
    }
});

I understand that you can publish a collection to the client, but that's not really as specific of a function I'm looking for, I don't want to publish the entire Player object to the client, I just want to tell the client to write a line to a textbox saying something like "You were hit for 12 damage by a monster!". 我知道你可以将一个集合发布到客户端,但这并不是我正在寻找的函数的特定内容,我不想将整个Player对象发布到客户端,我只是想告诉客户端在文本框中写一条线,上面写着“你被怪物击中了12点伤害!”。

I was hoping there was a function similar to SocketIO where I could, if I wanted to, just emit an event to the client telling it to update the UI. 我希望有一个类似于SocketIO的功能,如果我愿意的话,只需向客户端发出一个事件,告诉它更新UI。 I think I can use SocketIO for this if I need to, but people seemed to be adamant that something like this was doable with Meteor entirely without SocketIO, I just don't really understand how. 如果我需要的话,我想我可以使用SocketIO,但人们似乎坚持认为这样的事情完全没有SocketIO就可以使用Meteor,我只是不明白怎么做。

The only outs I see to this scenario are: writing all of the game logic client-side which feels like a bad idea, writing all of the combat logs to a collection which seems extremely excessive (but maybe it's not?), or using some sort of SocketIO type-tool to just emit messages to the client to tell it to write a new line to the text box. 我在这种情况下看到的唯一出局是:编写所有游戏逻辑客户端,感觉这是一个坏主意,将所有战斗日志写入一个似乎非常过分的集合(但可能不是?),或使用一些一种SocketIO类型工具,只是向客户端发出消息,告诉它写一个新行到文本框。

Using Meteor, create a combat log collection seem to be the simplest option you have. 使用Meteor,创建战斗日志集合似乎是您拥有的最简单的选项。 You can only listen on added event and then clear the collection when the combat is over. 您只能收听added事件,然后在战斗结束时清除集合。 It should be something like this : 它应该是这样的:

var cursor = Combat_Log.find();
var handleCombatLog = cursor.observe({
  added: function (tmp)
  {
    // do your stuff
  }
});

I ask a similar question here , hope this will help ^^ 在这里问一个类似的问题,希望这会有所帮助^^

Here's how I did it without a collection. 这是我没有收藏的方式。 I think you are right to be concerned about creating one. 我认为你关心创造一个是正确的。 That would not be a good idea. 那不是一个好主意。 First install Streamy. 首先安装Streamy。

https://atmospherejs.com/yuukan/streamy https://atmospherejs.com/yuukan/streamy

Then on the server 然后在服务器上

    //find active sockets for the user by id
    var sockets = Streamy.socketsForUsers(USER_ID_HERE)._sockets
    if (!Array.isArray(sockets) || !sockets.length) {
        //user is not logged in
    } else {
        //send event to all open browser windows for the user
        sockets.forEach((socket) => {
            Streamy.emit('ddpEvent', { yourKey:yourValue }, socket);
        })
    }

Then in the client, respond to it like this: 然后在客户端中,像这样回复它:

Streamy.on('ddpEvent', function(data) {
    console.log("data is ", data); //prints out {yourKey:yourValue}
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM