forked from ChatPlug/libfb-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
153 additions
and
116 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,16 @@ | ||
import Long from 'long' | ||
|
||
export default class RandomIntGenerator { | ||
// static generate () { | ||
// return Math.floor(Math.random() * (Math.pow(2, 32) + 1)) | ||
// } | ||
static getAttemptId (): Long { | ||
const sysTime: Long = Long.fromNumber(Date.now()).shiftLeft(22) | ||
const randomBit: Long = Long.fromNumber(RandomIntGenerator.generate() & 4194303).and(Long.MAX_VALUE) | ||
return sysTime.or(randomBit) | ||
} | ||
|
||
static generate () { | ||
return Math.floor(Math.random() * (Math.pow(2, 32) + 1)) | ||
return Math.floor(Math.random() * Math.floor(Number.MAX_SAFE_INTEGER)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,59 @@ | ||
import { FacebookMessageType } from './messages/MessageTypes' | ||
import { MessageType } from './messages/MessageTypes' | ||
|
||
/** | ||
* Represents an outgoing message. This class allows to buffer different kinds of binary data so that messages can be assembled. | ||
*/ | ||
export default class MqttMessage { | ||
toSend: Buffer | ||
type: FacebookMessageType | ||
type: MessageType | ||
flags: number | ||
|
||
constructor () { | ||
constructor (type?: MessageType) { | ||
this.toSend = Buffer.alloc(0) | ||
if (type) this.type = type | ||
} | ||
|
||
setFlags (flags: number) { | ||
this.flags = flags | ||
return this | ||
} | ||
|
||
writeU16 (data: number) { | ||
const newBuf = Buffer.alloc(2) | ||
newBuf.writeUInt16BE(data, 0) | ||
this.toSend = Buffer.concat([this.toSend, newBuf]) | ||
return this | ||
} | ||
|
||
writeU32 (data: number) { | ||
const newBuf = Buffer.alloc(4) | ||
newBuf.writeUInt32BE(data, 0) | ||
this.toSend = Buffer.concat([this.toSend, newBuf]) | ||
return this | ||
} | ||
|
||
writeU8 (data: number) { | ||
const newBuf = Buffer.alloc(1) | ||
newBuf.writeUInt8(data, 0) | ||
this.toSend = Buffer.concat([this.toSend, newBuf]) | ||
return this | ||
} | ||
|
||
writeString (strToAdd: string) { | ||
this.writeU16(strToAdd.length) | ||
const newBuf = Buffer.from(strToAdd, 'utf8') | ||
this.toSend = Buffer.concat([this.toSend, newBuf]) | ||
return this | ||
} | ||
|
||
writeRawString (strToAdd: string) { | ||
const newBuf = Buffer.from(strToAdd, 'utf8') | ||
this.toSend = Buffer.concat([this.toSend, newBuf]) | ||
return this | ||
} | ||
|
||
writeRaw (raw: Buffer) { | ||
this.toSend = Buffer.concat([this.toSend, raw]) | ||
return this | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,14 @@ | ||
import { MqttMessageFlag } from './MqttTypes' | ||
import { MessageType } from './messages/MessageTypes' | ||
|
||
export interface MqttHeader { | ||
size: number | ||
i: number | ||
} | ||
|
||
export enum FacebookMessageType { | ||
Connect = 1, | ||
ConnectAck = 2, | ||
Publish = 3, | ||
PublishAck = 4, | ||
PublishRecorded = 5, | ||
PublishReleased = 6, | ||
PubComp = 7, | ||
Subscribe = 8, | ||
SubscribeAck = 9, | ||
Unsubscribe = 10, | ||
UnsubscribeAck = 11, | ||
Ping = 12, | ||
Pong = 13, | ||
Disconnect = 14 | ||
} | ||
|
||
export default interface MqttPacket { | ||
header: MqttHeader | ||
type: FacebookMessageType | ||
type: MessageType | ||
content: Buffer | ||
flag: MqttMessageFlag | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
export enum FacebookMessageType { | ||
export enum MessageType { | ||
Connect = 1, | ||
ConnectAck = 2, | ||
Publish = 3, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,11 @@ | ||
import MqttMessage from '../MqttMessage' | ||
import { FacebookMessageType } from '../MqttPacket' | ||
import { MessageType } from './MessageTypes' | ||
import { MqttMessageFlag } from '../MqttTypes' | ||
|
||
/** | ||
* Assembles a ping message. | ||
*/ | ||
export const encodePing = (): MqttMessage => { | ||
const message = new MqttMessage() | ||
message.flags = 0 | ||
message.type = FacebookMessageType.Ping | ||
return message | ||
return new MqttMessage(MessageType.Ping) | ||
.setFlags(MqttMessageFlag.QoS0) | ||
} |
Oops, something went wrong.