如何在Go中使用带有NewServeMux的websocket?(How to use websocket with NewServeMux in Go?)
我收到一条错误消息:无法在mx.HandleFunc的参数中使用websocket.Handler(handler)(类型websocket.Handler)作为类型func(http.ResponseWriter,* http.Request)
在以下代码中
package main import ( "golang.org/x/net/websocket" "net/http" ) func handler(ws *websocket.Conn) { // TODO } func main() { mx := http.NewServeMux() mx.HandleFunc("/ws", websocket.Handler(handler)) // error http.ListenAndServe("localhost:8888", mx) }
I got an error message: cannot use websocket.Handler(handler) (type websocket.Handler) as type func(http.ResponseWriter, *http.Request) in argument to mx.HandleFunc
in following code
package main import ( "golang.org/x/net/websocket" "net/http" ) func handler(ws *websocket.Conn) { // TODO } func main() { mx := http.NewServeMux() mx.HandleFunc("/ws", websocket.Handler(handler)) // error http.ListenAndServe("localhost:8888", mx) }
原文:https://stackoverflow.com/questions/46520738
最满意答案
似乎你在错误的项目上使用扩展操作符。 你应该使用它来代替:
var newstate = Object.assign({}, state, {items: [...state.items, action.payload.found]} );
你的代码
{items: [state.items, ...action.payload.found]}
实际上试图传播作为对象的action.payload.found
,然后返回一个数组,其中第一项是旧数组,后面跟着数值从action.payload.found
例如,假设原始的
state.items
是[A, B, C]
,action.payload.found
是{id: "100", price: 10}
,然后{items: [state.items, ...action.payload.found]}
实际上会返回[[A, B, C], "100", 10]
。 但是,您希望它返回[A, B, C, {id: "100", price: 10}]
。 因此你需要传播state.items
。Seems you are using spread operator on wrong item. You should use this instead:
var newstate = Object.assign({}, state, {items: [...state.items, action.payload.found]} );
Your code
{items: [state.items, ...action.payload.found]}
actually tried to spreadaction.payload.found
which is an object and then returned an array where the first item was the old array followed by the values fromaction.payload.found
For instance assume original
state.items
was[A, B, C]
andaction.payload.found
was{id: "100", price: 10}
, then{items: [state.items, ...action.payload.found]}
would actually return[[A, B, C], "100", 10]
. However you want it to return[A, B, C, {id: "100", price: 10}]
instead. Therefore you need to spreadstate.items
.
相关问答
更多-
我是否通过替换对象中的整个数组来改变我的reducer状态?(Am I mutating my reducer state by replacing an entire array in an object?)[2022-10-21]
Object.assign创建一个新对象,将属性从一个(或多个)对象轻微复制到一个新对象中。 这意味着如果您的状态具有嵌套对象,则它们将从新对象引用(尽管它是一个新对象,如果从一个属性中删除它不会影响另一个属性)。 如果你想要不变性,我建议你使用Immutable.js Object.assign creates a new object shallowly copying the properties from one (or many) objects into a new one. This mean ... -
这将更新数据。 如果它匹配数组中的任何对象。 export const dataReducer = (state= InitialState , action = null) => { switch(action.type) { case types.UPDATE_DATA: return Object.assign({}, state, { data: state.data.map(item => { ...
-
由于历史记录为null ,并且null没有push方法,所以会引发错误。 添加一个后备初始化item || [] item || [] ,如果item为null ,则会为历史记录分配一个新数组: case SET_HISTORY: return state .updateIn(['data', payload.item, 'history'], // concat will return a new array, push will return the length of the ...
-
似乎你在错误的项目上使用扩展操作符。 你应该使用它来代替: var newstate = Object.assign({}, state, {items: [...state.items, action.payload.found]} ); 你的代码{items: [state.items, ...action.payload.found]}实际上试图传播作为对象的action.payload.found ,然后返回一个数组,其中第一项是旧数组,后面 ...
-
通过减速器将项目添加到子对象的子对象中的数组(Adding item to array in child object of a child object via reducer)[2022-05-14]
items: [ ...state.items, action.payload ] 在上面的摘录中,您试图将...state.items传播到items数组中,但其中的项目实际上是在state.order.cart.items ; 同理: cart: { ...state.cart, } cart实际上是在state.order.cart 像这样深嵌套的情况是为什么最好尽可能地平整你的状态并使用combineReducers() 。 或者,你可以看一个不可改变的帮手,以帮助不变地设置深度嵌套状 ... -
你的减速器有一个错字。 代替 switch (action.types) { 应该读 switch (action.type) { There's a typo in your reducer. Instead of switch (action.types) { should read switch (action.type) {
-
Reducer中的ArrayList问题(ArrayList Issue in Reducer)[2022-05-18]
像@Patricia Shanahan已经注意到,对象正在被重用 - 对象的底层内容正在被更新(但所有的子对象等也被重用,这取决于你的readFields / write方法)。 在将它们添加到ArrayList之前,你可以解决这个问题: @Override public void reduce(Key_class key, Iteratorvalues, OutputCollector output, Reporter reporter) ... -
您应该为defaultList中的对象添加一个id: const defaultList = [ { id: 1, recipe: 'Pizza', ingredients: ['tomato-sauce','cheese','peperoni'] }, { id: 2, recipe: 'Pie', ingredients: ['dough','cherry'] }, { id: 3, recipe: 'Curry', ingredients: ['rice','sauce','carrots ...
-
在您的函数mapStateToProps您尝试将cat设置为state.cats但在combineReducers函数中,您的对象看起来像{catReducer: catReducer} 。 尝试将combineReducers函数中的catReducer条目更改为{cats: catReducer} In your function mapStateToProps you tried to asign cats to state.cats but in your combineReducers funct ...
-
例如,每个动作[GET_POSTS]是传播state和action.payload并返回结果值; 是的, {...state位将{...state所有属性传播到一个新对象。 此语法也类似于Object.assign或_.merge ,右边的参数与左边的现有参数合并(如果它们是相同的属性则覆盖。 也许如果我们制作手动扩展,这将更有意义。 用这些: const state = { a: 1, b: 2, c: 3 }; const action = { payload: { c: 4 }}; 所以这: { . ...