oslabs-beta / sono.land

Real-time Communication Library for Deno (WebSockets & WebRTC)
https://sono.land
148 stars 24 forks source link

how do i use sono with oak #8

Open monotter opened 2 years ago

monotter commented 2 years ago

i made below code to make sono connection with oak

import { SONO , OAK } from './Dependencies.ts'
const { Application } = OAK
const { Sono } = SONO
const app = new Application()
const sono = new Sono()
app.listen({ port: parseInt(Deno.env.get('PORT')!) })
app.use((context, next) => {
    if (context.request.method == 'GET' && context.request.url.pathname == '/sono') {
        sono.connect(context.request.originalRequest, () => {
            sono.emit('new client connected')
        })
    } else { next() }
})

./Dependencies.ts

import * as SONO from 'https://deno.land/x/sono@v1.1/mod.ts'
import * as OAK from 'https://deno.land/x/oak@v10.1.1/mod.ts'
export { SONO, OAK }

but it wont work because oak's context.request.originalRequest does not includes bufWriter and bufReader

shah commented 2 years ago

This is a great library! I have the same problem with Oak, any guidance from Sono core team?

ansarizafar commented 2 years ago

I also like to use Sono with Oak. Why there is no response from core team? Is this project dead?

lorrenbiffin commented 1 month ago

I'm also interested in Oak support. Is support for this use-case planned? If not, is there an unsupported workaround?

For visibility, error output:

$ deno run -A src\main.ts
Server is running on http://localhost:9339
Request: Request {
  hasBody: false,
  headers: Headers {
    "accept-encoding": "gzip, deflate, br, zstd",
    "accept-language": "en-US,en;q=0.9",
    "cache-control": "no-cache",
    connection: "Upgrade",
    host: "localhost:9339",
    origin: "http://localhost:9339",
    pragma: "no-cache",
    "sec-websocket-extensions": "permessage-deflate; client_max_window_bits",
    "sec-websocket-key": "1hp25SiMNg7l+d5U4a21KA==",
    "sec-websocket-version": "13",
    upgrade: "websocket",
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Sa"... 25 more characters
  },
  ip: "127.0.0.1",
  ips: [],
  method: "GET",
  secure: false,
  url: "http://localhost:9339/ws"
}
TypeError: Cannot read properties of undefined (reading 'write')
    at writeAll (https://deno.land/std@0.96.0/io/util.ts:159:25)
    at BufWriter.flush (https://deno.land/std@0.96.0/io/bufio.ts:470:13)
    at writeResponse (https://deno.land/std@0.96.0/http/_io.ts:292:16)
    at eventLoopTick (ext:core/01_core.js:168:7)
    at async acceptWebSocket (https://deno.land/std@0.96.0/ws/mod.ts:447:5) err

The code:

import * as path from "https://deno.land/std/path/mod.ts";

import { Sono } from "https://deno.land/x/sono@v1.1/mod.ts";

import { Application, Router, send, Context } from "https://deno.land/x/oak@v12.5.0/mod.ts";

const app = new Application();
const router = new Router();
const sono = new Sono();

router.get("/ws", (context: Context) => {
    if (!context.isUpgradable) {
        context.throw(501);
    }

    try {
        console.log(`Request:`, context.request);

        sono.connect(context.request, () => {
            sono.emit("new client connected");
        });
    } catch (error) {
        console.error(`Faled to create Sono connection:`, error);
        context.response.status = 500;
        context.response.body = "Server Error";
    }
});

app.use(router.routes());
app.use(router.allowedMethods());

app.use(async (context) => {
    try {
        const rootDir = path.resolve(Deno.cwd());
        await send(context, context.request.url.pathname, {
            root: `${rootDir}/public`,
            index: "index.html",
        });
    } catch (error) {
        console.error(`Failed to serve static content:`, error);
        context.response.status = 404;
        context.response.body = "Not Found! </3";
    }
});

const port = 9339;
console.log(`Server is running on http://localhost:${port}`);
await app.listen({ port });