Fix creating several heartbeat threads
This commit is contained in:
parent
77e5a74141
commit
44d52848aa
|
@ -37,7 +37,8 @@ You can view examples in the [examples](examples) directory.
|
||||||
# Todo:
|
# Todo:
|
||||||
- [x] Finish all REST API calls.
|
- [x] Finish all REST API calls.
|
||||||
- [x] Handle all gateway events.
|
- [x] Handle all gateway events.
|
||||||
- [x] Reconnecting
|
- [x] Reconnecting.
|
||||||
|
- [ ] Configurable Logger.
|
||||||
- [ ] Add library to official Nimble package repository.
|
- [ ] Add library to official Nimble package repository.
|
||||||
- [ ] Memory optimizations.
|
- [ ] Memory optimizations.
|
||||||
- [ ] Member
|
- [ ] Member
|
||||||
|
|
|
@ -120,8 +120,11 @@ proc handleGatewayDisconnect(shard: Shard, error: string) {.async.} =
|
||||||
|
|
||||||
#TODO: Reconnecting may be done, just needs testing.
|
#TODO: Reconnecting may be done, just needs testing.
|
||||||
proc handleWebsocketPacket(shard: Shard) {.async.} =
|
proc handleWebsocketPacket(shard: Shard) {.async.} =
|
||||||
|
var hasStartedHeartbeatThread = false;
|
||||||
while true:
|
while true:
|
||||||
|
|
||||||
|
# Skip if the websocket isn't open
|
||||||
|
if shard.ws.readyState == Open:
|
||||||
var packet = await shard.ws.receiveStrPacket()
|
var packet = await shard.ws.receiveStrPacket()
|
||||||
shard.client.log.debug("[SHARD " & $shard.id & "] Received gateway payload: " & $packet)
|
shard.client.log.debug("[SHARD " & $shard.id & "] Received gateway payload: " & $packet)
|
||||||
|
|
||||||
|
@ -160,10 +163,14 @@ proc handleWebsocketPacket(shard: Shard) {.async.} =
|
||||||
shard.heartbeatInterval = json["d"]["heartbeat_interval"].getInt()
|
shard.heartbeatInterval = json["d"]["heartbeat_interval"].getInt()
|
||||||
await shard.sendGatewayRequest(shard.getIdentifyPacket())
|
await shard.sendGatewayRequest(shard.getIdentifyPacket())
|
||||||
|
|
||||||
# Don't start a new
|
# Don't start a new heartbeat thread if one is already started
|
||||||
if (not shard.isHandlingHeartbeat):
|
echo "About to start a heartbeat thread! shard.heartbeatAcked is ", shard.heartbeatAcked
|
||||||
|
if not hasStartedHeartbeatThread:
|
||||||
|
echo "Starting new heartbeat thread! shard.heartbeatAcked is ", shard.heartbeatAcked
|
||||||
asyncCheck shard.handleHeartbeat()
|
asyncCheck shard.handleHeartbeat()
|
||||||
shard.heartbeatAcked = true
|
hasStartedHeartbeatThread = true
|
||||||
|
else:
|
||||||
|
echo "Not gonna start a new heartbeat thread since. shard.heartbeatAcked is ", shard.heartbeatAcked
|
||||||
of ord(DiscordOpCode.opHeartbeatAck):
|
of ord(DiscordOpCode.opHeartbeatAck):
|
||||||
shard.heartbeatAcked = true
|
shard.heartbeatAcked = true
|
||||||
of ord(DiscordOpCode.opDispatch):
|
of ord(DiscordOpCode.opDispatch):
|
||||||
|
@ -233,12 +240,15 @@ proc startConnection*(client: DiscordClient, shardAmount: int = 1) {.async.} =
|
||||||
|
|
||||||
shard.ws = await newWebSocket(shard.client.endpoint & "/v=6&encoding=json")
|
shard.ws = await newWebSocket(shard.client.endpoint & "/v=6&encoding=json")
|
||||||
|
|
||||||
asyncCheck shard.handleWebsocketPacket()
|
await shard.handleWebsocketPacket()
|
||||||
|
|
||||||
# Just wait. Don't poll while we're reconnecting
|
# Just wait. Don't poll while we're reconnecting
|
||||||
while true:
|
#[ while true:
|
||||||
if not shard.reconnecting:
|
if not shard.reconnecting:
|
||||||
|
try:
|
||||||
poll()
|
poll()
|
||||||
|
except WebSocketError:
|
||||||
|
echo "WebSocketError" ]#
|
||||||
else:
|
else:
|
||||||
raise newException(IOError, "Failed to get gateway url, token may of been incorrect!")
|
raise newException(IOError, "Failed to get gateway url, token may of been incorrect!")
|
||||||
|
|
||||||
|
|
Reference in New Issue