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

example doesn't work #38

Open
ralyodio opened this issue Dec 28, 2017 · 8 comments
Open

example doesn't work #38

ralyodio opened this issue Dec 28, 2017 · 8 comments

Comments

@ralyodio
Copy link

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
Copy link
Author

bump

@ofaucorp
Copy link

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

@Yugloocamai
Copy link

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

@kudos
Copy link
Owner

kudos commented Apr 24, 2019

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

Repository owner deleted a comment from Aravin Aug 20, 2020
@jitbasemartin
Copy link

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
Copy link

This still works with @koa/router

@ralyodio
Copy link
Author

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

@jdalrymple
Copy link

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

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

6 participants