Skip to content

Commit

Permalink
fix things not executing in main thread
Browse files Browse the repository at this point in the history
  • Loading branch information
NikkyAI committed Feb 4, 2021
1 parent 82383ef commit e0675d1
Showing 1 changed file with 43 additions and 22 deletions.
65 changes: 43 additions & 22 deletions src/WebsocketHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,15 @@ public void Close(bool skipMessage = false)
{
if (Config.SendApiConnectEvents && !skipMessage)
{
SendSystemMessage(
text: Config.TEXT_ServerStop,
@event: ApiMessage.EventJoinLeave,
gateway: Config.generalGateway
);

Api.Event.EnqueueMainThreadTask(() =>
{
SendSystemMessage(
text: Config.TEXT_ServerStop,
@event: ApiMessage.EventJoinLeave,
gateway: Config.generalGateway
);
}, "vs-matterbridge");
}

_reconnectWebsocket = false;
Expand All @@ -97,7 +101,8 @@ public void SendSystemMessage(string text, string gateway, string @event = "")
);
}

public void SendUserMessage(IServerPlayer player, string text, string gateway, bool generateAvatar, string @event = "")
public void SendUserMessage(IServerPlayer player, string text, string gateway, bool generateAvatar,
string @event = "")
{
SendMessage(
username: player.PlayerName,
Expand All @@ -109,7 +114,8 @@ public void SendUserMessage(IServerPlayer player, string text, string gateway, b
);
}

public void SendUserMessage(string playerName, string playerUid, string text, string gateway, bool generateAvatar, string @event = "")
public void SendUserMessage(string playerName, string playerUid, string text, string gateway,
bool generateAvatar, string @event = "")
{
SendMessage(
username: playerName,
Expand Down Expand Up @@ -249,33 +255,48 @@ private void websocket_MessageReceived(object sender, MessageReceivedEventArgs e
.ToArray()
);

if (groupUid == null)
{
Mod.Logger.Error("groupId was null");
return;
}

switch (message.Event)
{
case ApiMessage.EventJoinLeave:
{
Api.SendMessageToGroup(
groupUid,
$"<strong>{message.Username}</strong>: {cleanedMessageText}",
EnumChatType.OthersMessage
);
Api.Event.EnqueueMainThreadTask(() =>
{
Api.SendMessageToGroup(
groupUid,
$"<strong>{message.Username}</strong>: {cleanedMessageText}",
EnumChatType.OthersMessage
);
}, "vs-matterbridge");
break;
}
case ApiMessage.EventUserAction:
{
Api.SendMessageToGroup(
groupUid,
$"<strong>{message.Username}</strong> <i>{cleanedMessageText}</i>",
EnumChatType.OthersMessage
);
Api.Event.EnqueueMainThreadTask(() =>
{
Api.SendMessageToGroup(
groupUid,
$"<strong>{message.Username}</strong> <i>{cleanedMessageText}</i>",
EnumChatType.OthersMessage
);
}, "vs-matterbridge");
break;
}
case "":
{
Api.SendMessageToGroup(
groupUid,
$"<strong>{message.Username}</strong>: {cleanedMessageText}",
EnumChatType.OthersMessage
);
Api.Event.EnqueueMainThreadTask(() =>
{
Api.SendMessageToGroup(
groupUid,
$"<strong>{message.Username}</strong>: {cleanedMessageText}",
EnumChatType.OthersMessage
);
}, "vs-matterbridge");
break;
}
default:
Expand Down

0 comments on commit e0675d1

Please sign in to comment.