我有服务器端
import io from 'socket.io';
export function startServer(store){
const server = new io(7000);
store.subscribe(() => {
server.sockets.emit("state", store.getState().toJS());
})
server.on('conneсtion', socket => {
console.log(`${socket.handshake.address} has joined`)
socket.emit("state", store.getState().toJS());
socket.on("action", store.dispatch.bind(store));
});
}
和客户端
import React from 'react';
import ReactDOM from 'react-dom';
import {VotingContainer} from './components/Voting';
import {BrowserRouter, Route} from 'react-router-dom';
import App from './components/App';
import {ResultsContainer} from './components/Results';
import {createStore, applyMiddleware} from 'redux';
import reducer from './reducer';
import {fromJS} from 'immutable';
import {Provider} from 'react-redux';
import io from 'socket.io-client';
import {setState} from './actionCreators';
import remoteActionMiddleware from './remoteActionMiddleware';
import {Map, List} from 'immutable';
const socket = new io('localhost:7000');
socket.on('state', state => {
console.log(state);
store.dispatch(setState(state))
});
console.log(socket);
socket.on('connect', () => {
alert('connected');
})
const createStoreWithMiddleWare = applyMiddleware(remoteActionMiddleware)
(createStore);
const store = createStoreWithMiddleWare(reducer);
ReactDOM.render(
<Provider store={store}>
<BrowserRouter>
<Route component={App}/>
</BrowserRouter>
</Provider>,
document.getElementById("root")
)
首先我启动服务器
babel-node index.js
然后通过webpack-dev-server启动客户端。
最终事件“连接”在客户端激发,但在服务器端事件“连接”不会触发。所以我不能将状态从服务器转移到客户端
这是什么原因?