简体   繁体   English

数组或对象,适合使用节点js进行实时聊天

[英]array or object which one is adapt for live chat using node js

I am learning about nodejs + express + socketio . 我正在学习有关nodejs + express + socketio的知识 I am making live chat for customer. 我正在为客户进行实时聊天。 I have successfully get user list in Admin page. 我已经在“管理”页面中成功获取用户列表 currently Admin page show all user sending message but I need to show only selected user message from user list. 当前的“管理员”页面显示所有正在发送用户的消息,但我只需要显示从用户列表中选择的用户消息。

In this case I need one array or object which have all details about chat session but in my case details are coming from different events so I confused how to push the value. 在这种情况下,我需要一个数组或对象 ,该数组或对象具有有关聊天会话的所有详细信息,但是在我的情况下,这些详细信息是来自不同的事件,所以我很困惑如何推值。

When I search about array and object I found Arrays have order, objects don't but the statement is confusing me. 当我搜索数组和对象时,我发现数组是有序的,对象却没有,但是该语句使我感到困惑。 upto now I can use object but I could not felt any difference between object and array except syntax. 到目前为止,我可以使用对象,但是除了语法之外,我还没有感觉到对象和数组之间的任何区别。

Here I put two questions 我在这里提出两个问题

  1. Which one right for handle private chat (Array or object)? 处理私人聊天(数组或对象)的哪一项权利?
  2. How to push the data uniformly from different events for example after triggering three events my object or array should like below 如何从不同事件中统一推送数据,例如在触发三个事件后,我的对象或数组应如下所示

     chat={"name":"Bilal","mail":"test@test.com","socketid":"asgd","to":"John","message":"Hello!","ip":"192.168.1.4","time":"timestamp"}; 

Okay so first let me tell you there is a lot you still need to read up about nodejs and programming in general to start with this project or it will confuse you. 好的,首先让我告诉您,从这个项目开始,您仍然需要大量阅读有关nodejs和编程的知识,否则它会让您感到困惑。

Coming back to your question, the distinction between array and object in terms of having order is that - In simple terms think of "having order" as being able to number the items and retrieve them using an index. 回到您的问题,就顺序而言,数组和对象之间的区别在于-简单来说,“具有顺序”是指能够对项目进行编号并使用索引来检索它们。 This is possible with arrays but not with objects. 对于数组,这是可能的,但对于对象,这是不可能的。 For example you have an array a and object o , a[5] will give you the 6th element of a but o[5] doesn't make any sense. 例如,您有一个数组aa对象oa[5]将为您提供a的第6个元素,但是o [5]没有任何意义。 An object just holds pieces together in no specific order. 一个对象只是没有特定顺序地将各个部分放在一起。

IMHO its best to communicate between methods using objects especially in nodejs. 恕我直言,最好在使用对象的方法之间进行通信,尤其是在nodejs中。 You also have the flexibility to use other complex structures when working with objects. 您还可以灵活地在处理对象时使用其他复杂的结构。

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

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