我正在我的应用中使用SignalR
,React
前端设置Redux
。我能够将消息发送到我的集线器,但是当集线器广播消息时,我在控制台中看到以下内容:
警告:找不到名为“UpdateMessages”的客户端方法。
这是我的中间件看起来像是负责坐在SignalR中心和我的Redux
商店之间。此代码位于其自己的单独文件中:
import * as types from '../../actions/actionTypes';
import * as SignalR from '@aspnet/signalr-client';
// Get the store
import appStore from '../../store/appStore';
// Declare connection
let connection = new SignalR.HubConnection('http://localhost:49065/im', 'formatType=json&format=text');
export const signalRInvokeMiddleware = (store) => (next) => async (action) => {
switch (action.type) {
case types.SIGNALR_SEND_MESSAGE:
connection.invoke("Send", action.message);
break;
}
return next(action);
}
export const signalRRegisterCommands = (appStore) => {
connection.on('UpdateMessages', data => {
debugger;
appStore.dispatch(updateSignalRMessages(data))
})
connection.start();
};
connection.start();
以下是我的商店代码:
import {createStore, applyMiddleware} from 'redux';
import thunk from 'redux-thunk';
import rootReducer from '../reducers/rootReducer';
import { signalRInvokeMiddleware } from '../middleware/signalR/signalRMiddleware';
export default function configureStore(initialState){
return createStore(
rootReducer,
initialState,
applyMiddleware(thunk, signalRInvokeMiddleware)
);
}
这就是我的集线器代码:
public class MyChatHub : Hub
{
public Task Send(string message)
{
return Clients.All.InvokeAsync("UpdateMessages", message);
}
}
更新:我发现了问题。我不得不改变我的configureStore()
。见下文:
export default function configureStore(initialState){
const store = createStore(
rootReducer,
initialState,
applyMiddleware(thunk, signalRInvokeMiddleware)
);
signalRRegisterCommands(store);
return store;
}