简体   繁体   English

如何在meteor.js和react.js上编写一个简单的聊天,没有数据库?

[英]How to write a simple chat, without a database on meteor.js and react.js?

I want to write a simple chat on meteor.js and thus I do not want to store the data in the database . 我想在meteor.js上写一个简单的聊天,因此我不想将数据存储在database But I never found how to make an application without a database . 但我从未发现如何在没有database情况下创建应用程序。

Here is an example of code as I can imagine. 这是我能想象的代码示例。 Server code: 服务器代码:

export let ws = [{_id:'1', text:'test1'}, {_id:'2', text:'test2'}];
Meteor.publish('ws', function wsPub() { return ws; });
let ctr = 3;
Meteor.methods({
    'addMsg'(text) {  ws.push({_id:ctr+1, text:text});  }
});

and client code: 和客户代码:

import {ws} from '../api/model.js';

class Rtc extends Component {
  constructor(props) {
    super(props);
  }
  addMsg(e){
    e.preventDefault();
    Meteor.call('addMsg', this.refs.input.value);
  }
  render() {
    return (
      <div>
         {this.props.ws.map((item, i)=>{ 
           return(<span key={i._id}>{item.text}</span>); 
         })}
         <input type="text" ref="input" />
         <input type="submit" value="submit" onClick={this.addMsg.bind(this)}/>
       </div>
    );
  }
}
export default createContainer( () => {
  Meteor.subscribe('ws');
  return { ws: ws };
}, Rtc);

but I do not understand what I wrote is not so in the createContainer ? 但我不明白我在createContainer写的不是这样吗?

UPD: I updated server code, but still websockets does not work: UPD:我更新了服务器代码,但仍然无法使用websockets:

Meteor.publish('ws', function wsPub() {
  let self = this;
  ws.forEach( (msg)=> {
    self.added( "msg", msg._id, msg.text );
  });
  self.ready();
  // return ws;
});

What you suppose won't work. 你认为不会起作用。 Because Meteor.publish returns a cursor to a Collection or array of Collections . 因为Meteor.publish返回一个光标到一个CollectionarrayCollections According to the official documentation : 根据官方文件

Publish functions can return a Collection.Cursor, in which case Meteor will publish that cursor's documents to each subscribed client. 发布函数可以返回Collection.Cursor,在这种情况下,Meteor会将该游标的文档发布到每个订阅的客户端。 You can also return an array of Collection.Cursors, in which case Meteor will publish all of the cursors. 您还可以返回一组Collection.Cursors,在这种情况下,Meteor将发布所有游标。

Again, when you subscribe to a publication, it stores the data(as cursor to the same collection as the publication) locally in MiniMongo . 同样,当您订阅发布时,它会在MiniMongo中本地存储数据(作为游标与发布相同的集合)。 So a chat without database is not technically possible with pub-sub in Meteor. 因此,在Meteor中使用pub-sub技术上无法进行无数据库的聊天。

If you want to control what is sent over a publish, get a reference to the "publish instance" (really a specific client with a specific subscription) and use its add / change / remove commands: 如果要控制通过发布发送的内容,请获取对“发布实例”(实际上是具有特定订阅的特定客户端)的引用,并使用其add / change / remove命令:

let messages = [];
let clients = [];
Meteor.publish('ws', function() {
    clients.push(this);
    _.each(messages, (message) => {this.added('msg', message._id, message);});
    this.ready();
});
Meteor.methods({
    addMsg(text) {
        let newMessage = {_id: Meteor.uuid(), text: text};
        messages.push(newMessage);
        _.each(clients, (client) => {client.added('msg', newMessage._id, newMessage);});
    }
});

Regarding your code that you wrote in an update: you're sending a string where the function added expects a document (an object ). 关于您在更新中编写的代码:您正在发送一个string ,其中added的函数需要文档( object )。 Also, unlike this example above, you do not tell the clients when the ws (messages array) has changed. 此外,与上面的示例不同,您不会在ws (消息数组)发生更改时告诉客户端。

I'd recommend also renaming these things to be more verbose and clear :) 我建议也将这些东西重命名为更详细和清晰:)

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

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