Skip to content

Commit

Permalink
Extremely rough implementation of WebSocket stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
TTTaevas committed Nov 29, 2023
1 parent af87fc3 commit 0c6de56
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 5 deletions.
10 changes: 10 additions & 0 deletions lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import fetch, { FetchError } from "node-fetch"
import WebSocket from "ws"
import querystring from "querystring"

import { User, UserWithKudosu, UserWithCountry, UserWithCountryCover, UserWithCountryCoverGroupsStatisticsrulesets, UserWithCountryCoverGroupsStatisticsSupport,
Expand Down Expand Up @@ -280,6 +281,15 @@ export class API {
}
}

public generateWebSocket(): WebSocket {
return new WebSocket(`${this.server.replace(/https{0,1}:\/\/\w*/g, "wss://notify")}`, [], {
headers: {
"User-Agent": "osu-api-v2-js (https://github.com/TTTaevas/osu-api-v2-js)",
"Authorization": `${this.token_type} ${this.access_token}`
}
})
}

/**
* @param method The type of request, each endpoint uses a specific one (if it uses multiple, the intent and parameters become different)
* @param endpoint What comes in the URL after `api/`
Expand Down
24 changes: 20 additions & 4 deletions lib/tests/test_authorized.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { exec } from "child_process"
import util from "util"

const prompt = promptSync({sigint: true})
const server = "https://dev.ppy.sh"
const server = "https://osu.ppy.sh"

function sleep(seconds: number) {
return new Promise(resolve => setTimeout(resolve, seconds * 1000))
Expand All @@ -21,10 +21,26 @@ async function test(id: string | undefined, secret: string | undefined, redirect
if (secret === undefined) {throw new Error("no SECRET env var")}
if (redirect_uri === undefined) {throw new Error("no REDIRECT_URI env var")}

let url = osu.generateAuthorizationURL(Number(id), redirect_uri, ["public", "chat.read", "chat.write", "chat.write_manage"], server)
let url = osu.generateAuthorizationURL(Number(id), redirect_uri, ["public", "chat.read", "chat.write_manage"], server)
exec(`xdg-open "${url}"`)
let code = prompt(`What code do you get from: ${url}\n\n`)
let api = await osu.API.createAsync({id: Number(id), secret}, {code, redirect_uri}, "all", server)
let api = await osu.API.createAsync({id: Number(id), secret}, {code, redirect_uri}, "errors", server)

// Proof web socket stuff work well
let socket = api.generateWebSocket()
socket.on("open", () => {
socket.send(JSON.stringify({event: "chat.start"}))
api.keepChatAlive()
setInterval(() => api.keepChatAlive(), 30 * 1000)
})
socket.on("message", (m: any) => {
let event = JSON.parse(m.toString())
if (event.event === "chat.message.new") {
let message = event.data.messages.map((message: any) => message.content).join(" | ")
let user = event.data.users.map((user: any) => user.username).join(" | ")
console.log(`${user}: ${message}`)
}
})
}

test(process.env.DEV_ID, process.env.DEV_SECRET, process.env.REDIRECT_URI)
test(process.env.ID, process.env.SECRET, process.env.REDIRECT_URI)
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"devDependencies": {
"@types/node": "^20.8.10",
"@types/prompt-sync": "^4.2.2",
"@types/ws": "^8.5.10",
"ajv": "^8.12.0",
"dotenv": "^16.3.1",
"prompt-sync": "^4.2.0",
Expand All @@ -29,6 +30,7 @@
"typescript": "^5.2.2"
},
"dependencies": {
"node-fetch": "^3.3.2"
"node-fetch": "^3.3.2",
"ws": "^8.14.2"
}
}
19 changes: 19 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@
resolved "https://registry.yarnpkg.com/@types/json-schema/-/json-schema-7.0.15.tgz#596a1747233694d50f6ad8a7869fcb6f56cf5841"
integrity sha512-5+fP8P8MFNC+AyZCDxrB2pkZFPGzqQWUzpSeuuVLvm8VMcorNYavBqoFcxK8bQz4Qsbn4oUEEem4wDLfcysGHA==

"@types/node@*":
version "20.10.0"
resolved "https://registry.yarnpkg.com/@types/node/-/node-20.10.0.tgz#16ddf9c0a72b832ec4fcce35b8249cf149214617"
integrity sha512-D0WfRmU9TQ8I9PFx9Yc+EBHw+vSpIub4IDvQivcp26PtPrdMGAq5SDcpXEo/epqa/DXotVpekHiLNTg3iaKXBQ==
dependencies:
undici-types "~5.26.4"

"@types/node@^20.8.10":
version "20.9.2"
resolved "https://registry.yarnpkg.com/@types/node/-/node-20.9.2.tgz#002815c8e87fe0c9369121c78b52e800fadc0ac6"
Expand All @@ -19,6 +26,13 @@
resolved "https://registry.yarnpkg.com/@types/prompt-sync/-/prompt-sync-4.2.3.tgz#b6a9fe88fc4b4cacb8ab59f87f2614d6e674e177"
integrity sha512-Ox77gCSx0YyeakGt/qfOZUSFNSSi+sh3ABoGOiCwiO2KODx492BJnUm9oIXS+AHJtqp12iM4RduY6viTJ9bYwA==

"@types/ws@^8.5.10":
version "8.5.10"
resolved "https://registry.yarnpkg.com/@types/ws/-/ws-8.5.10.tgz#4acfb517970853fa6574a3a6886791d04a396787"
integrity sha512-vmQSUcfalpIq0R9q7uTo2lXs6eGIpt9wtnLdMv9LVpIjCA/+ufZRozlVoVelIYixx1ugCBKDhn89vnsEGOCx9A==
dependencies:
"@types/node" "*"

ajv@^8.12.0:
version "8.12.0"
resolved "https://registry.yarnpkg.com/ajv/-/ajv-8.12.0.tgz#d1a0527323e22f53562c567c00991577dfbe19d1"
Expand Down Expand Up @@ -283,3 +297,8 @@ wrappy@1:
version "1.0.2"
resolved "https://registry.yarnpkg.com/wrappy/-/wrappy-1.0.2.tgz#b5243d8f3ec1aa35f1364605bc0d1036e30ab69f"
integrity sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ==

ws@^8.14.2:
version "8.14.2"
resolved "https://registry.yarnpkg.com/ws/-/ws-8.14.2.tgz#6c249a806eb2db7a20d26d51e7709eab7b2e6c7f"
integrity sha512-wEBG1ftX4jcglPxgFCMJmZ2PLtSbJ2Peg6TmpJFTbe9GZYOQCDPdMYu/Tm0/bGZkw8paZnJY45J4K2PZrLYq8g==

0 comments on commit 0c6de56

Please sign in to comment.