Skip to content

feat(plugins): add c2.WebSocket - #6076

Merged
pajlada merged 40 commits into
Chatterino:masterfrom
Nerixyz:feat/plugin-ws
Apr 7, 2025
Merged

pajlada merged 40 commits into
Chatterino:masterfrom
Nerixyz:feat/plugin-ws

Conversation

@Nerixyz

@Nerixyz Nerixyz commented Mar 14, 2025 •

Copy link
Copy Markdown
Contributor

Adds a small WebSocket client. Connections must use TLS.

Example

Go in a Twitch channel and run /mirror-chat. All chat messages will now appear twice.

---@type c2.WebSocket|nil
local ws = nil
c2.register_command("/mirror-chat", function(ctx)
    if ws then
        ws:close()
    end
    local proto = "wss"
    if #ctx.words >= 2 then
        proto = ctx.words[2]
    end
    ws = c2.WebSocket.new(proto .. "://irc-ws.chat.twitch.tv")
    local chan = ctx.channel
    ws.on_text = function(text)
        -- Don't care about multiple messages in one
        text = text:sub(1, -3)
        if #text > 4 and text:sub(1, 4) == 'PING' then
            ws:send_text("PONG" .. text:sub(5))
            return
        end

        local sender_end = text:find(' ', 1, true)
        if sender_end == nil then
            return
        end
        local sender = text:sub(2, sender_end - 1)

        local command_end = text:find(' ', sender_end + 1, true)
        if command_end == nil then
            return
        end
        local command = text:sub(sender_end + 1, command_end - 1)
        if command ~= "PRIVMSG" then
            return
        end

        local name_end = sender:find('!', 1, true)
        if name_end == nil then
            return
        end
        local sender_name = sender:sub(1, name_end - 1)

        local msg_start = text:find(':', command_end + 1, true)
        if msg_start == nil then
            return
        end
        chan:add_system_message(sender_name .. ": " .. text:sub(msg_start + 1))
    end
    ws:send_text("NICK justinfan123456")
    ws:send_text("JOIN #" .. ctx.channel:get_name())
    chan:add_system_message("Mirroring messages")
end)

Requests: #4931, #4999 (comment)

@Mm2PL

Mm2PL commented Mar 14, 2025

Copy link
Copy Markdown
Collaborator

How did you decide on the TLS requirement? It seems weird to not be able to connect to another local program.

@Nerixyz

Nerixyz commented Mar 14, 2025

Copy link
Copy Markdown
Contributor Author

How did you decide on the TLS requirement? It seems weird to not be able to connect to another local program.

Because of Boost.Beast and that you need to statically declare the types of streams. It's not super hard to add non-TLS streams. I could do it later.

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clang-tidy made some suggestions

There were too many comments to post at once. Showing the first 25 out of 27. Check the log or trigger a new build to see more.

Comment thread src/controllers/plugins/api/WebSocket.hpp
Comment thread src/controllers/plugins/api/WebSocket.hpp
Comment thread src/controllers/plugins/api/WebSocket.hpp
Comment thread src/singletons/WebSocketPool.cpp Outdated
Comment thread src/singletons/WebSocketPool.cpp Outdated
Comment thread src/singletons/WebSocketPool.cpp Outdated
Comment thread src/singletons/WebSocketPool.cpp Outdated
Comment thread src/singletons/WebSocketPool.cpp Outdated
Comment thread src/singletons/WebSocketPool.cpp Outdated
Comment thread src/singletons/WebSocketPool.cpp Outdated
@Nerixyz

Nerixyz commented Mar 14, 2025

Copy link
Copy Markdown
Contributor Author

I could do it later.

Added.

@Nerixyz
Nerixyz marked this pull request as ready for review March 15, 2025 11:41
Comment thread .cirrus.yml Outdated
Comment thread docs/chatterino.d.ts
Comment thread src/controllers/plugins/PluginController.hpp
Comment thread src/controllers/plugins/SolTypes.cpp
Comment thread src/util/PostToThread.hpp

@pajlada pajlada left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

haven't looked through it all yet

Comment thread src/controllers/plugins/api/WebSocket.cpp
Comment thread src/controllers/plugins/api/WebSocket.cpp
Comment thread src/controllers/plugins/SolTypes.hpp
@pajlada
pajlada enabled auto-merge (squash) April 6, 2025 10:02
auto-merge was automatically disabled April 6, 2025 10:43

Head branch was pushed to by a user without write access

@pajlada
pajlada enabled auto-merge (squash) April 6, 2025 10:46
auto-merge was automatically disabled April 6, 2025 11:06

Head branch was pushed to by a user without write access

@pajlada
pajlada enabled auto-merge (squash) April 6, 2025 11:19
@pajlada
pajlada disabled auto-merge April 6, 2025 11:36
@pajlada
pajlada enabled auto-merge (squash) April 7, 2025 17:17
@pajlada
pajlada merged commit ab66be2 into Chatterino:master Apr 7, 2025
@Nerixyz
Nerixyz deleted the feat/plugin-ws branch April 7, 2025 17:46
@2547techno 2547techno mentioned this pull request May 3, 2025
4 tasks done
babicekmarek pushed a commit to babicekmarek/streamally-chatterino that referenced this pull request Sep 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants