PageRenderTime 57ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/js/model/NetChannelMessage.js

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
JavaScript | 78 lines | 28 code | 8 blank | 42 comment | 4 complexity | 28059badd533036d0895002e2923b213 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, MPL-2.0-no-copyleft-exception, BSD-3-Clause
  1. /**
  2. File:
  3. NetChannelMessage.js
  4. Created By:
  5. Mario Gonzalez
  6. Project:
  7. RealtimeMultiplayerNodeJS
  8. Abstract:
  9. A common class for sending messages between ServerNetChannel / ClientNetChannel
  10. It is a small value-object wrapper which contains a sequence number and clientid
  11. Basic Usage:
  12. // Create the data that will live in the message
  13. var command = {};
  14. // Fill in the data
  15. command.cmd = aCommandConstant;
  16. command.data = {x:1, y:1};
  17. // Create the message
  18. var message = new Message(this.outgoingSequence, true, command)
  19. // Send the message (can happen later on)
  20. message.messageTime = this.realTime; // Store to determin latency
  21. (from netchannel)
  22. this.connection.send(message.encodedSelf());
  23. };
  24. */
  25. (function () {
  26. RealtimeMultiplayerGame.namespace("RealtimeMultiplayerGame.model");
  27. /**
  28. * Creates a NetChannelMessage
  29. * @param aSequenceNumber A sequence number, used to track messages between server / client
  30. * @param isReliable A message is 'reliable' if it must be sent, for example fireweapon / disconnect. It is 'unreliable', if it can be overwritten with newer data, i.e. currentPosition
  31. * @param aPayload The message to send
  32. */
  33. //var message = new RealtimeMultiplayerGame.model.NetChannelMessage( this.outgoingSequenceNumber, this.clientID, isReliable, aCommandConstant, payload );
  34. RealtimeMultiplayerGame.model.NetChannelMessage = function (aSequenceNumber, aClientid, isReliable, aCommandType, aPayload) {
  35. // Info
  36. this.seq = aSequenceNumber;
  37. this.id = aClientid; // Server gives us one when we first connect to it
  38. this.cmd = aCommandType;
  39. // Data
  40. this.payload = aPayload;
  41. // State
  42. this.messageTime = -1;
  43. this.isReliable = isReliable;
  44. };
  45. RealtimeMultiplayerGame.model.NetChannelMessage.prototype = {
  46. // This message MUST be sent if it is 'reliable' (Connect / Disconnect).
  47. // If not it can be overwritten by newer messages (for example moving is unreliable, because once it's outdates its worthless if new information exist)
  48. isReliable: false,
  49. cmd: 0,
  50. aPayload: null,
  51. seq: -1,
  52. id: -1,
  53. messageTime: -1,
  54. /**
  55. * Wrap the message with useful information before sending, optional BiSON or something can be used to compress the message
  56. */
  57. encodeSelf: function () {
  58. if (this.id == -1) {
  59. console.log("(Message) Sending message without clientid. Note this is ok, if it's the first message to the server.");
  60. }
  61. if (this.messageTime == -1) {
  62. console.log("(Message) Sending message without messageTime. Expected result is undefined");
  63. }
  64. return {id: this.clientid, seq: this.sequenceNumber, cmds: this.unencodedMessage, t: this.messageTime}
  65. }
  66. }
  67. })()