kudos / koa-websocket

Light wrapper around Koa providing a websocket middleware handler that is koa-route compatible.
MIT License
257 stars 36 forks source link

example doesn't work #38

Open ralyodio opened 6 years ago

ralyodio commented 6 years ago
const koa = require('koa');
const websockify = require('koa-websocket');
const router = require('koa-router');
const app = new koa();
const api = router();
const socket = websockify(app);

api.get('/*', function* (next) {
    this.websocket.send('Hello World');
    this.websocket.on('message', function(message) {
        console.log(message);
    });
});

app.ws.use(api.routes()).use(api.allowedMethods());
app.listen(3000);

When I try to debug with wscat -c ws://localhost:3000 nothing happens :(

ralyodio commented 6 years ago

bump

ofaucorp commented 6 years ago

same here. i searched and saw some discussion about this. any update?

Yugloocamai commented 5 years ago

Seems this project is broken, I'm having the same issue :(

kudos commented 5 years ago

koa-router is not explicitly supported, but patches with tests are welcome.

jitbasemartin commented 3 years ago

This following example is working for me

const router = require('koa-router')();
const websockify = require('koa-websocket');
const Koa = require('koa');

const app = websockify(new Koa());

router.get('/ping', async (ctx) => {
  ctx.websocket.send('Hello world');
  ctx.websocket.on('message', (message) => {
    console.log(message);
    ctx.websocket.send(message);
  });
});

app.ws.use(router.routes()).use(router.allowedMethods());
app.listen(3000);

Then test with wscat:

$ wscat --connect ws://localhost:3000/ping                                                                                         *[feature/JWA-3699-user-i-want-to-have-an-api-endp] 
Connected (press CTRL+C to quit)
< Hello world
> Test
< Test
>

My package.json

  "dependencies": {
    ...
    "koa": "2.13.0",
    "koa-router": "9.4.0",
    "koa-websocket": "^6.0.0",
    ...
  }
jdalrymple commented 2 years ago

This still works with @koa/router

ralyodio commented 2 years ago

Does it work with latest version though? I don’t see how it suddenly works.

jdalrymple commented 2 years ago

hahah honestly, your guess is as good as mine. Im using it right now without any issues