Reactjs WebSocket onmessage连接为空

Reactjs WebSocket onmessage连接为空,reactjs,redux,websocket,redux-saga,Reactjs,Redux,Websocket,Redux Saga,我收到一条空的.onmessage 但是,onopen正在工作 import * as types from './types'; import { eventChannel } from 'redux-saga'; import { takeEvery, put, call, select, take } from 'redux-saga/effects'; export function* createEventChannel() { const mySocket = new Web

我收到一条空的
.onmessage
但是,onopen正在工作

import * as types from './types';
import { eventChannel } from 'redux-saga';
import { takeEvery, put, call, select, take } from 'redux-saga/effects';


export function* createEventChannel() {
  const mySocket = new WebSocket('wss://url.com');
  return eventChannel((emit: any) => {
    mySocket.onopen = () => {
      mySocket.send("sdsdsdsd!");
    };
// =====> onmessage is getting null
    mySocket.onmessage((message:any) => {
        emit(message.data)
    });

    return () => {
      mySocket.close();
    };
    
  });
}

export function* workWebSocket(action:any) {
  const channel = yield call(createEventChannel);
  while (true) {
    const { message } = yield take(channel);
    yield put({ type: types.WEB_SOCKET_SUCCESS, message: message });
  }
}

export default function* watchHydration() {
  yield takeEvery(types.WEB_SOCKET_FETCH, workWebSocket);
}

关于我为什么会变为null有什么建议吗?

看看这个例子:你能正确解析数据吗

function initWebsocket() {
return eventChannel(emitter => {
ws = new WebSocket(wsUrl + '/client')
ws.onopen = () => {
  console.log('opening...')
  ws.send('hello server')
}
ws.onerror = (error) => {
  console.log('WebSocket error ' + error)
  console.dir(error)
}
ws.onmessage = (e) => {
  let msg = null
  try {
    msg = JSON.parse(e.data)
  } catch(e) {
    console.error(`Error parsing : ${e.data}`)
  }
  if (msg) {
    const { payload: book } = msg
    const channel = msg.channel
    switch (channel) {
      case 'ADD_BOOK':
        return emitter({ type: ADD_BOOK, book })
      case 'REMOVE_BOOK':
        return emitter({ type: REMOVE_BOOK, book })
      default:
        // nothing to do
    }
  }
}
// unsubscribe function
return () => {
  console.log('Socket off')
 }
})
}
export default function* wsSagas() {
const channel = yield call(initWebsocket)
 while (true) {
  const action = yield take(channel)
  yield put(action)
}
 }

是的,我确实正确地解析了数据。但是我发现我在消息的arrow函数上犯了一个错误,这就是我在mySocket.onMessage=function((message:any)=>{emit(message.data)})上犯了一个错误的地方我很高兴这很有帮助。我还发现了
mySocket.onmessage