I’m developing a custom bits widget, and I can’t seem to properly interpret an incoming Bits message, despite having read the JSON template and trying to interpret accordingly.
Before getting into the problem, here is my socket handler layer:
function InterpretMessage(message) {
var parsed = JSON.parse(message.data);
// Other message type handlers.
if (parsed.type == "MESSAGE")
{
for (var i = 0; i < messageCallback.length; i++)
{
if (messageCallback[i].topic == parsed.data.topic)
{
messageCallback[i].callback(parsed.data.message);
return;
}
}
}
}
This layer simply receives a message, and then looks for a callback associated with the given topic. “message” as received by the function is a MessageEvent object.
When I ask my socket handler to listen to a topic, I provide a callback to associate to it. In the case of bits, I provide the following function as the callback:
function InterpretData(message)
{
var username = message.user_name;
var bitsUsed = message.bits_used;
var action = message.context;
if (nextBoss == "")
{
// Performs a web call to obtain the display name of the person who sent bits.
// Once finished, the webcall function then calls the
GetUserInfo(message.user_name, function(info) {
var amount = "";
if (message.bits_used < 100) { amount = "1"; }
else if (message.bits_used < 1000) { amount = "100"; }
else if (message.bits_used < 5000) { amount = "1000"; }
else if (message.bits_used < 10000) { amount = "5000"; }
else { amount = "10000"; }
// Do some document manipulation
Strike(username, bitsUsed, info.displayName);
});
}
}
The problem I’m having is that the Strike command, when dealing with real bits, is always given either null or undefined values. In the demo mode I have, these values are fine since I’m creating the objects myself. This tells me that the message object received by PubSub is not the same as the message object I create for my demo, and also not the same as the message object the twitch docs say I will receive. (shown here: https://dev.twitch.tv/docs/v5/guides/PubSub/bits/ )
Since I can’t receive bits, I cannot test this with a real bits message.