Javascript AspNet核心信号器和Redux配置-发送和接收数据

Javascript AspNet核心信号器和Redux配置-发送和接收数据,javascript,redux,react-redux,asp.net-core-signalr,ecmascript-next,Javascript,Redux,React Redux,Asp.net Core Signalr,Ecmascript Next,我正在实现一个聊天功能,它使用AspNet Core SiginalR和React.js+Redux 解决问题的步骤 我可以通过信令器为后端服务器发送消息 我可以通过中间件接收回消息 但是我不能调度更新存储中的状态和更新视图 问题 我做错了什么 可能我无法从回调连接访问分派函数。在(“ReceiveMessage”,data=> 是吗?怎么修 应用程序 导入“@fake db” 从'React'导入React,{suspent}; 从'@fuse'导入{FuseAuthorization,Fu

我正在实现一个聊天功能,它使用AspNet Core SiginalR和React.js+Redux

解决问题的步骤
我可以通过信令器为后端服务器发送消息
我可以通过中间件接收回消息
但是我不能调度更新存储中的状态和更新视图

问题
我做错了什么

可能我无法从回调连接访问分派函数。在(“ReceiveMessage”,data=>

是吗?怎么修

应用程序


导入“@fake db”
从'React'导入React,{suspent};
从'@fuse'导入{FuseAuthorization,FuseLayout,FuseTheme};
从“react-redux/es/components/Provider”导入提供程序;
从“react Router dom”导入{Router};
从“jss扩展”导入jssExtend;
从“@history”导入历史;
从“/Auth”导入{Auth};
从“./store”导入存储;
从“./AppContext”导入AppContext;
从“/fuse configs/routeConfig”导入路由;
从“jss”导入{create};
从'@material ui/styles'导入{StylesProvider,jssPreset,createGenerateClassName};
从“axios”导入axios;
const jss=create({
…jssPreset(),
插件:[…jssPreset().插件,jssExtend()],
insertionPoint:document.getElementById('jss-insertion-point'),
});
axios.defaults.baseURL='0https://localhost:5001/api/v1/';
const generateClassName=createGenerateClassName();
常量应用=()=>{
返回(

您正在调用action creator,但实际上并没有分派它在中间件中生成的操作,因此它只创建了一个不做任何事情的对象

-    export default function signalRMiddleware() { 
+    export default function signalRMiddleware(api) { 

            connection.on('ReceiveMessage', data => {
-                Actions.receiveSocketMessage(data)
+                api.dispatch(Actions.receiveSocketMessage(data))
            })

            return  next => action => {

            switch (action.type) {

                case Actions.DIRECT_MESSAGE: 
                    connection.invoke('DirectMessage', action.payload);
            }

            return next(action);
        }
    }

您正在调用action creator,但实际上并没有分派它在中间件中生成的操作,因此它只创建了一个不做任何事情的对象

-    export default function signalRMiddleware() { 
+    export default function signalRMiddleware(api) { 

            connection.on('ReceiveMessage', data => {
-                Actions.receiveSocketMessage(data)
+                api.dispatch(Actions.receiveSocketMessage(data))
            })

            return  next => action => {

            switch (action.type) {

                case Actions.DIRECT_MESSAGE: 
                    connection.invoke('DirectMessage', action.payload);
            }

            return next(action);
        }
    }
解决方案 信号管理软件

从'@microsoft/signalr'导入{HubConnectionBuilder,LogLevel};
从“../../main/apps/chat/store/Actions”导入*作为操作;
const connection=new HubConnectionBuilder()。带URL(“https://localhost:5001/chatHub")
.configureLogging(日志级别信息)
.withAutomaticReconnect()
.build();
connection.start();
导出默认函数信号管理软件(api){
connection.on('ReceiveMessage',data=>{
log(`cheguei no middleware-msg:${data}`);
分派({type:Actions.RECEIVE_MESSAGE,payload:data});
})
返回下一步=>action=>{
开关(动作类型){
案例操作。直接消息:
{
log(`enviando msg:${action.payload.message}`);
connection.invoke('DirectMessage',action.payload);
}
}
返回下一步(操作);
}
}
创建存储

import*作为“redux”中的reduxModule;
从“redux”导入{applyMiddleware,compose,createStore};
从“/reducers”导入createReducer;
从“./middleware/signalrmidware”导入signalrmidware;
从“redux thunk”导入thunk;
/*
修复Firefox redux开发工具扩展
https://github.com/zalmoxisus/redux-devtools-instrument/pull/19#issuecomment-400637274
*/
reduxModule.\u不使用\u操作类型。替换='@@redux/INIT';
康斯特康塞恩汉斯酒店=
process.env.NODE_env!=“生产”&&
窗口的类型===“对象”&&
窗口。\u REDUX\u DEVTOOLS\u EXTENSION\u COMPOSE\u?
窗口。\uuuu REDUX\u DEVTOOLS\u EXTENSION\u COMPOSE__({
//指定扩展的选项,如名称、ActionsBlakList、actionsCreators、序列化。。。
}):撰写;
常量增强子=复合增强子(
applyMiddleware(
砰,
信号处理软件
)
//其他商店增强器(如有)
);
const store=createStore(createReducer(),enhancer);
store.asyncReducers={};
export const injectReducer=(键,reducer)=>{
if(store.asyncReducers[key])
{
返回;
}
store.asyncReducers[key]=减速机;
replaceReducer(createReducer(store.asyncReducers));
退货店;
};
导出默认存储;
解决方案 信号管理软件

从'@microsoft/signalr'导入{HubConnectionBuilder,LogLevel};
从“../../main/apps/chat/store/Actions”导入*作为操作;
const connection=new HubConnectionBuilder()。带URL(“https://localhost:5001/chatHub")
.configureLogging(日志级别信息)
.withAutomaticReconnect()
.build();
connection.start();
导出默认函数信号管理软件(api){
connection.on('ReceiveMessage',data=>{
log(`cheguei no middleware-msg:${data}`);
分派({type:Actions.RECEIVE_MESSAGE,payload:data});
})
返回下一步=>action=>{
开关(动作类型){
案例操作。直接消息:
{
log(`enviando msg:${action.payload.message}`);
connection.invoke('DirectMessage',action.payload);
}
}
返回下一步(操作);
}
}
创建存储

import*作为“redux”中的reduxModule;
从“redux”导入{applyMiddleware,compose,createStore};
从“/reducers”导入createReducer;
从“./middleware/signalrmidware”导入signalrmidware;
从“redux thunk”导入thunk;
/*
修复Firefox redux开发工具扩展
https://github.com/zalmoxisus/redux-devtools-instrument/pull/19#issuecomment-400637274
*/
reduxModule.\u不使用\u操作类型。替换='@@redux/INIT';
康斯特康塞恩汉斯酒店=
process.env.NODE_