whatwewant / dva-socket.io

A socket.io plugin for dva
14 stars 5 forks source link

Request: Make io object a constructor dependency #4

Open PabloReszczynski opened 6 years ago

PabloReszczynski commented 6 years ago

This would allow to use any version of socket.io with this plugin and also allow the use of socket.io mocks for testing.

PabloReszczynski commented 6 years ago

Example:

// Simple Usage
import dva from 'dva';
import createSocket from 'dva-socket.io';
import io from 'socket.io-client';

const app = dva();

app.use(createSocket('http://127.0.0.1:8080', null, io, {
  // when server push an server-message event,
  //  it will dispatch an action use server data,
  on: ['server-message'],
  emit: [
    // when you dispatch an action with type === 'send-message',
    //  it will emit a client-message event with data('client send a message')
    [
      'client-message',
      (action) => action.type === 'send-message',
      'client send a message',
    ],
  ],
}));
Elvin1993 commented 6 years ago

use this version

https://github.com/elvin-gogo/dva-socket.io

createDvaSocket(url, options, rules, ref_io); // ref_io is target socket.io
whatwewant commented 6 years ago

@PabloReszczynski Awesome idea. I have saw that maybe no everybody want the same version of socket.io-client. So the next version will add custom io. If you have done that, give a pr, thanks.

And another question: mocks for testing, can you give me an example, which show what you want ?

whatwewant commented 6 years ago

@Elvin1993 Awesome. It looks good.

elvin-gogo commented 6 years ago

mock for testing is use this one mock-socket

PabloReszczynski commented 6 years ago

With a mock socket, I could use all the features of dva-socket.io without having to use a real socket connection, very useful for testing. mock-socket is a well known library that does it.

whatwewant commented 6 years ago

Thanks. I know.

About custom socket.io version or mock-socket, @elvin-gogo @Elvin1993 have committed a good pr #5 . I have released a new version 0.0.6. Maybe you can try it. I will renew the docs latter.