如果我写{transports: ['websocket']}
,那么我会出错-
WebSocket连接到 'ws://echo.websocket.org/socket.io/?EIO = 3&transport = websocket'失败: WebSocket握手期间出错:意外的响应代码:404
否则-
在以下位置访问XMLHttpRequest 'http://echo.websocket.org/socket.io/?EIO=3&transport=polling&t=Mi560SU' 来自来源“ http://localhost:3000”的信息已被CORS政策阻止: 请求中没有'Access-Control-Allow-Origin'标头 资源。
我的传奇:
import { put, take, call } from 'redux-saga/effects';
import io from 'socket.io-client';
import { eventChannel } from 'redux-saga';
import {
GET_TEST_SAGA,
} from '../actions';
export const socket = io.connect('http://echo.websocket.org', { transports: ['websocket'] });
const SocketEvents = {
jobsFresh: 'jobs+fresh',
};
function createFreshJobsChannel() {
const subscribe = (emitter) => {
socket.on(SocketEvents.jobsFresh, emitter);
return () => socket.removeListener(SocketEvents.jobsFresh, emitter);
};
return eventChannel(subscribe);
}
export default function* getTestSocket() {
const channel = yield call(createFreshJobsChannel);
while (true) {
yield take(GET_TEST_SAGA);
while (true) {
const jobs = yield take(channel);
console.log(jobs);
}
}
}