Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Enhancement] Add optional websocket support to the app #14

Open
jfmherokiller opened this issue Apr 14, 2020 · 0 comments
Open

[Enhancement] Add optional websocket support to the app #14

jfmherokiller opened this issue Apr 14, 2020 · 0 comments

Comments

@jfmherokiller
Copy link
Contributor

I have a few ideas for webapps that allow live editing/controling the switch but sys-botbase doesnt have an accessable websocket port.

I am rn trying to see if i can add that using the wslay library.

for now have a typescript connection example

import {PromiseSocket}  from "promise-socket"
import {Socket} from "net";

async function Peek(socket:PromiseSocket<Socket>, address: number, size: number): Promise<Buffer> {
    await socket.write(PeekCommandConstruct(address,size))
    let buffer = Buffer.from((await socket.read()).toString(),'hex').reverse();
    return buffer
}
async function Poke(socket:PromiseSocket<Socket>,address:number,size:number,data:Buffer) {
    let Command = PokeCommandConstruct(address,size,data);
    await socket.write(Command);
}

async function MySocketFun() {
    const socket = new PromiseSocket();
    await socket.connect({
        port: 6000,
        host: "192.168.1.102",
    });

    //test Simple Commands
    const SimpleCommands = ["getTitleID", "getSystemLanguage", "getHeapBase"];
    for (let Command of SimpleCommands) {
        await socket.write(CommandConstruct(Command));
        const response = (await socket.read()) as Buffer
        console.log(`${Command}:${response.toString().trim()}`);
    }
    //peak test
    let result = await Peek(socket,0xAC3B90C0,1024);
    console.log(result.toString('hex'));
    await socket.end();
    socket.destroy();
    //test Poke
    await Poke(socket,0xAC3B90C4,4,Buffer.from([0,0,0,0]))
}

function CommandConstruct(Command: string) {
    return Command + "\r\n";
}

function PeekCommandConstruct(offset: number, size: number) {
    if (Number.isInteger(offset) && Number.isInteger(size)) {
        const Soffset = `0x${offset.toString(16).toUpperCase()}`;
        const SoffsetAndSize = `${Soffset} ${size.toString()}`;
        const Final = `peek ${SoffsetAndSize}`;
        return CommandConstruct(Final);
    } else {
        throw TypeError;
    }
}

function PokeCommandConstruct(offset: number, size: number, data: Buffer) {
    if (Number.isInteger(offset) && Number.isInteger(size)) {
        const Soffset = `0x${offset.toString(16).toUpperCase()}`;
        const SoffsetAndSize = `${Soffset} ${size.toString()}`;
        const Final = `poke ${SoffsetAndSize} ${data.toString('hex')}`;
        return CommandConstruct(Final);
    } else {
        throw TypeError;
    }
}

MySocketFun().catch(console.error);
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

No branches or pull requests

1 participant