Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

小程序无法添加header #16

Closed
rzrobert opened this issue Aug 15, 2018 · 4 comments
Closed

小程序无法添加header #16

rzrobert opened this issue Aug 15, 2018 · 4 comments

Comments

@rzrobert
Copy link

const sendHeader = {
"pt": "weixin",
"sv": "18",
"lg": "zh-CN"
};
onSocket = io.connect(server, {
path: "/" + path,
transports: ['polling'],
transportOptions: {
polling: {
extraHeaders: sendHeader
}
}
});

@ccckblaze
Copy link

ccckblaze commented Aug 23, 2018

got it, it turns out a bug inside the code, some magic comes out!

-------------------------------step 1----------------------------------
const socket = io(url, {
transportOptions: {
websocket: {
extraHeaders: {
'x-clientid': 'abc'
}
}
}
});

---------------------------------step 2----------------------------------
in "weapp.socket.io.js" replace
{ "content-type": "application/json" }
with
Object.assign({ "content-type": "application/json" }, r.headers)

@rzrobert

@ccckblaze
Copy link

How NewBee i am!

@gongzili456
Copy link
Member

@rzrobert weapp.socket.io not support polling transport

@gongzili456
Copy link
Member

gongzili456 commented Sep 13, 2018

@rzrobert @ccckblaze 关于 header 的使用,socket.io 官方文档有给出解释, 在 websokcet transport 模式下无法自定义 header,而且小程序版本的 socket.io 是不支持 polling transport 的。

With extraHeaders
This only works if polling transport is enabled (which is the default). Custom headers will not be appended when using websocket as the transport. This happens because the WebSocket handshake does not honor custom headers. (For background see the WebSocket protocol RFC)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants