Skip to content

Commit ae1a54b

Browse files
committed
Buffer chat messages, fix bugs, update TODOs.
Fix log event handler not being unregistered. Disable login start with certificates (for now).
1 parent 9597fc4 commit ae1a54b

4 files changed

Lines changed: 23 additions & 6 deletions

File tree

src/minecraft/connection/native.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ export class NativeServerConnection
133133
this.eventEmitter.removeAllListeners('ecm:packet')
134134
this.eventEmitter.removeAllListeners('ecm:error')
135135
this.eventEmitter.removeAllListeners('ecm:close')
136+
this.eventEmitter.removeAllListeners('ecm:log')
136137
this.closed = true
137138
this.emit('close')
138139
})
@@ -150,6 +151,7 @@ export class NativeServerConnection
150151
this.eventEmitter.removeAllListeners('ecm:packet')
151152
this.eventEmitter.removeAllListeners('ecm:error')
152153
this.eventEmitter.removeAllListeners('ecm:close')
154+
this.eventEmitter.removeAllListeners('ecm:log')
153155
}
154156
}
155157

src/minecraft/connection/shared.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ export const parseEncryptionRequestPacket = (packet: Packet) => {
3030
export const getLoginPacket = (opts: ConnectionOptions) => {
3131
const data: PacketDataTypes[] = [opts.username]
3232
if (opts.protocolVersion >= protocolMap[1.19]) {
33+
data.push(false)
34+
/* TODO: Support chat signing properly.
3335
data.push(!!opts.certificate)
3436
if (opts.certificate) {
3537
let buf = Buffer.alloc(8)
@@ -46,7 +48,7 @@ export const getLoginPacket = (opts: ConnectionOptions) => {
4648
buf = Buffer.from(opts.certificate.publicKeySignature, 'base64')
4749
data.push(writeVarInt(buf.byteLength))
4850
data.push(buf)
49-
}
51+
} */
5052
if (opts.protocolVersion >= protocolMap['1.19.1']) {
5153
if (opts.selectedProfile) {
5254
const msb = Buffer.from(opts.selectedProfile.substring(0, 16), 'hex')

src/screens/chat/ChatScreen.tsx

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ const ChatScreen = ({ navigation, route }: Props) => {
108108
const [messages, setMessages] = useState<Message[]>([])
109109
const [loggedIn, setLoggedIn] = useState(false)
110110
const [message, setMessage] = useState('')
111+
const messagesBufferRef = useRef<Message[]>([])
111112
const healthRef = useRef<number | null>(null)
112113
const statusRef = useRef<Status>(connection ? 'CONNECTING' : 'OPENING')
113114
const idRef = useRef(0)
@@ -117,16 +118,27 @@ const ChatScreen = ({ navigation, route }: Props) => {
117118
? 256
118119
: 100
119120
const addMessage = (text: MinecraftChat) =>
120-
setMessages(m => {
121-
const trunc = m.length > 500 ? m.slice(0, 499) : m
122-
return [{ key: idRef.current++, text }].concat(trunc)
123-
})
121+
messagesBufferRef.current.unshift({ key: idRef.current++, text })
124122
const closeChatScreen = () => {
125123
if (navigation.canGoBack() && statusRef.current !== 'CLOSED') {
126124
navigation.goBack()
127125
}
128126
}
129127

128+
// Chat message buffering function.
129+
useEffect(() => {
130+
const interval = setInterval(() => {
131+
if (messagesBufferRef.current.length) {
132+
setMessages(m => {
133+
const concat = messagesBufferRef.current.concat(m)
134+
messagesBufferRef.current = []
135+
return concat.length > 500 ? concat.slice(0, 499) : concat
136+
})
137+
}
138+
}, 50)
139+
return () => clearInterval(interval)
140+
}, [])
141+
130142
// Screen cleanup function.
131143
useEffect(() =>
132144
navigation.addListener('beforeRemove', () => {

src/screens/chat/sessionBuilder.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ export const createConnection = async (
4343
// Create an updated "session" containing access tokens and certificates.
4444
let session = sessions[activeAccount]
4545
const is119 = version >= protocolMap[1.19]
46+
// TODO: We should store session store in a persistent cache. Certificates and access tokens should be updated regularly.
4647
if (uuid && (!session || (!session.certificate && is119))) {
47-
// LOW-TODO: Certificates and access tokens should be updated regularly.
4848
// We should probably lock access to them via a semaphore.
4949
try {
5050
// Create a session with the latest access token.
@@ -91,6 +91,7 @@ export const createConnection = async (
9191
}
9292
}
9393

94+
// TODO: Better connection cancellation support. The session load can take a lot of time.
9495
// Connect to server after setting up the session.
9596
try {
9697
const newConn = await initiateConnection({

0 commit comments

Comments
 (0)