简体   繁体   English

通过websockets将消息添加到聊天组件(react.js)

[英]Add message to chat component(react.js) via websockets

Context: 语境:

I am developing a Ruby On Rails application and I started using React.js to manage my javascript components. 我正在开发一个Ruby On Rails应用程序,我开始使用React.js来管理我的javascript组件。 My application provides a facebook-like chat: several chats are displayed at the bottom of the page. 我的应用程序提供类似Facebook的聊天:页面底部显示几个聊天。

Problem 问题

I have a ChatList component that renders the chats. 我有一个ChatList组件来呈现聊天。 A chat is made of its messages and its form. 聊天消息及其形式。 When this form is submitted, an AJAX call is made to the server to POST the message and the message is added to the current chat. 提交此表单时,将向服务器发出AJAX调用以POST消息,并将消息添加到当前聊天中。

this.setState({messages: this.state.messages.concat([newMessage])});

The server then broadcast Javascript code to the receiver. 然后,服务器将Javascript代码广播到接收器。

This is where I'm stuck. 这就是我被困住的地方。 How can I add the message to the correct chat? 如何将消息添加到正确的聊天中? How can I select my React.js component and change its 'props'? 如何选择我的React.js组件并更改其“道具”? When I was not using react, I used to broadcast this code to the other user: 当我没有使用react时,我曾经将此代码广播给其他用户:

$("#chat-<%= chat.id %>").append("<%= message.content" %>);

I guess I have to find a way to select the React component (the chat instance) and change its property "messages". 我想我必须找到一种方法来选择React组件(聊天实例)并更改其属性“messages”。 How? 怎么样?

Thank you for your help! 谢谢您的帮助! :) :)

EDIT: I'm going to add more information, just in case: 编辑:我将添加更多信息,以防万一:

  • My ChatList is a global variable that takes an array of Chats. 我的ChatList是一个全局变量,它接受一个Chats数组。
  • Each Chat takes an array of Message and a form. 每个Chat都有一个Message和一个表单的数组。

When I submit the form of a chat, it adds the message to the Chat (locally and it also posts the new message to the server). 当我提交聊天表单时,它会将消息添加到聊天(本地,它还会将新消息发布到服务器)。 When the server receives the POST event, it can render javascript code to the other user. 当服务器收到POST事件时,它可以将javascript代码呈现给其他用户。

This code should add the message to the correct Chat for the correct user. 此代码应将消息添加到正确的用户的正确聊天中。 There are two pieces missing: 缺少两件:

  1. I don't know how I can "select" the Chat. 我不知道如何“选择”聊天。
  2. I don't know how I can add a message to the "messages" array of this Chat. 我不知道如何向此聊天的“messages”数组添加消息。

You'll want to have your chat component listen to a WebSocket event for a new message and then call setState when a message is received. 您希望让聊天组件侦听新消息的WebSocket事件,然后在收到消息时调用setState

The standard approach with Chatrooms is to use "channels" that each chat room subscribes to. Chatrooms的标准方法是使用每个聊天室订阅的“频道”。 That way it only hears about incoming messages from the conversation it cares about. 这样,它只会听到来自其关注的对话的传入消息。

Check out Socket.io and its example on making a chat application. 查看Socket.io及其制作聊天应用程序的示例。 Even if you don't use Socket.io the example will be illustrative. 即使您不使用Socket.io,该示例也将是说明性的。

The solution, in my Chat class: 解决方案,在我的Chat类中:

componentDidMount: function(){
      this.props.privateChannel.bind('message.new', this.receiveMessage);
  },
receiveMessage: function(message) {
    if(message.user.id == this.props.recipient.id){
      this.setState({messages: this.state.messages.concat([message])});
      this.startBlink();
    }
},

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

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