简体   繁体   English

Node.js WebSocket分页

[英]nodejs websocket pagination

I have an interface with javascript that requests connection to a websocket using nodejs. 我有一个使用javascript的接口,该接口请求使用nodejs连接到Websocket。 The websocket is frequently sending real time data that it produces to an interface. WebSocket经常将其产生的实时数据发送到接口。

What is the best way or best practice to make the pagination for the interface. 什么是进行界面分页的最佳方法或最佳实践。

Its not like i would append all data that server push to interface, the data is sent through websocket one by one, so its some kind like logging. 它不像我将服务器推送到接口的所有数据附加在一起,数据是通过websocket一对一发送的,所以它有点像日志记录。

This some example of the code 这是一些代码示例

connection.onmessage = function (message) {
  console.log(message.data);
  //var json = JSON.parse(message.data.text);
  content.prepend('<p><span style="color:red"></span> @ '+ message.data + '</p>');
  console.log(message.data.text);     
};

Any reference for this? 任何参考呢? Sorry for the bad english. 对不起,英语不好。

This example has a few more things going on then just the requested information about paging. 这个示例还有其他一些事情,然后只是请求的有关分页的信息。

  • Client Side, we are setting listRequest_Message , and passing the message to the Server's socket event. 在客户端,我们正在设置listRequest_Message ,并将消息传递到服务器的套接字事件。 SessionID is used only to know which user to respond to. SessionID仅用于知道要响应的用户。 UseIndexStart is the current min record, and UseIndexEnd is the current max record. UseIndexStart是当前的最小记录, UseIndexEnd是当前的最大记录。

     listRequest_Message = [[SessionID], [UseIndexStart], [UseIndexEnd]] 
  • The example returns 21 records per page 该示例每页返回21条记录

     //Page Back var UseIndexStart = StartIndex - 21 var UseIndexEnd = UseIndexStart + 22 //Page Forward var UseIndexStart = parseInt(StartIndex) + 21; var UseIndexEnd = UseIndexStart + 22; 

Server Application 服务器应用

  • Change the response to respond with records between the indexes. 更改响应以响应索引之间的记录。
  • The socket event that the client calls is dynamic, but I've included the one with paging below called listRequest_news 客户端调用的套接字事件是动态的,但是我在下面的分页中包括了一个名为listRequest_news
  • The JSON object being parsed is a text file containing a list of Movies, TV Shows, and general media. 解析的JSON对象是一个文本文件,其中包含电影,电视节目和常规媒体的列表。

code: 码:

socket.on('listRequest_news', function(msg, body){
for(var i = 0; i < mediaObj.length; i++){
  var thisobj = mediaObj[i];

  var JSONmsg = JSON.parse(msg[0]);
  var ClientIndex = clientkeyIndex.indexOf(JSONmsg);

  var startindex = msg[1];
  var endindex = msg[2];

  if (i > startindex && i < endindex)
    {
      clientID = parseInt(ClientIndex);
      client[clientID].emit('list_RequestGenericMedia', thisobj);
    }

  clientID = parseInt(ClientIndex);
  client[clientID].emit('list_RequestGenericMedia', thisobj);

}
});

Client Side 客户端

  • Store the StartIndex and EndIndex for the selected records 存储所选记录的StartIndexEndIndex
  • Attach those values to Previous and Next buttons 将这些值附加到上一个和下一个按钮

code: 码:

$("#listviewcontrolller_prev").on("click", "", function(event){

var selectedOption = localStorage.getItem('selectedOption')

var StartIndex = $("#prevpage").text();
var EndIndex = $("#nextpage").text();

var UseIndexStart = StartIndex - 21
var UseIndexEnd = UseIndexStart + 22

$("#prevpage").text(UseIndexStart);
$("#nextpage").text(UseIndexEnd);

var SessionID = localStorage.getItem('SocketID')
var listRequest_Message = [[SessionID], [UseIndexStart], [UseIndexEnd]];

$("#newsOutPut").empty();
socket.emit(selectedOption, listRequest_Message);
});

$("#listviewcontrolller_next").on("click", "", function(event){

var selectedOption = localStorage.getItem('selectedOption');

var StartIndex = $("#prevpage").text();
var EndIndex = $("#nextpage").text();

var UseIndexStart = parseInt(StartIndex) + 21;
var UseIndexEnd = UseIndexStart + 22;

$("#prevpage").text(UseIndexStart);
$("#nextpage").text(UseIndexEnd);

var SessionID = localStorage.getItem('SocketID')
var listRequest_Message = [[SessionID], [UseIndexStart], [UseIndexEnd]];

$("#newsOutPut").empty();
socket.emit(selectedOption, listRequest_Message);

});

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

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