PageRenderTime 81ms CodeModel.GetById 76ms app.highlight 2ms RepoModel.GetById 1ms 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
 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
18 // Create the message
19 var message = new Message(this.outgoingSequence, true, command)
20
21 // Send the message (can happen later on)
22 message.messageTime = this.realTime; // Store to determin latency
23
24 (from netchannel)
25 this.connection.send(message.encodedSelf());
26 };
27 */
28(function () {
29
30    RealtimeMultiplayerGame.namespace("RealtimeMultiplayerGame.model");
31
32    /**
33     * Creates a NetChannelMessage
34     * @param aSequenceNumber        A sequence number, used to track messages between server / client
35     * @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
36     * @param aPayload                The message to send
37     */
38        //var message = new RealtimeMultiplayerGame.model.NetChannelMessage( this.outgoingSequenceNumber, this.clientID, isReliable, aCommandConstant, payload );
39    RealtimeMultiplayerGame.model.NetChannelMessage = function (aSequenceNumber, aClientid, isReliable, aCommandType, aPayload) {
40        // Info
41        this.seq = aSequenceNumber;
42        this.id = aClientid; 					// Server gives us one when we first  connect to it
43        this.cmd = aCommandType;
44
45        // Data
46        this.payload = aPayload;
47
48        // State
49        this.messageTime = -1;
50        this.isReliable = isReliable;
51    };
52
53    RealtimeMultiplayerGame.model.NetChannelMessage.prototype = {
54        // This message MUST be sent if it is 'reliable' (Connect / Disconnect).
55        // 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)
56        isReliable: false,
57        cmd: 0,
58        aPayload: null,
59        seq: -1,
60        id: -1,
61        messageTime: -1,
62
63        /**
64         * Wrap the message with useful information before sending, optional BiSON or something can be used to compress the message
65         */
66        encodeSelf: function () {
67            if (this.id == -1) {
68                console.log("(Message) Sending message without clientid. Note this is ok, if it's the first message to the server.");
69            }
70
71            if (this.messageTime == -1) {
72                console.log("(Message) Sending message without messageTime. Expected result is undefined");
73            }
74
75            return {id: this.clientid, seq: this.sequenceNumber, cmds: this.unencodedMessage, t: this.messageTime}
76        }
77    }
78})()