问题
I am trying to create a tcp
socket in the background script of my app.
The error is (first line in 1.js
):
Uncaught TypeError: Cannot read property 'tcp' of undefined
Background script 1.js
:
chrome.sockets.tcp.create({}, function(createInfo) {
chrome.sockets.tcp.connect(createInfo.socketId,
"127.0.0.1", 4005, function(socketInfo) {
});
});
Manifest file :
{
"manifest_version": 2,
"name": "UDP TEST",
"version": "1.0",
"app": {
"background": {
"scripts": ["1.js"]
}
},
"permissions": [
{
"socket": [
"tcp-listen:*:*",
"tcp-connect",
"resolve-host"
]
}
]
}
Can anyone help me? Thanks!
回答1:
You have wrong permissions in manifest. Look up the Chrome API help: https://developer.chrome.com/apps/sockets_tcp (and specificaly for manifest: https://developer.chrome.com/apps/manifest/sockets)
The permissions should read "sockets". You are using the new "sockets" API, but in your manifest you are refering to old "socket" permissions (https://developer.chrome.com/apps/socket)
Your manifest permissions should read:
"permissions": [{
"sockets": {
"tcp": {
"connect": "127.0.0.1:4005"
}
}
}]
来源:https://stackoverflow.com/questions/28393105/why-chrome-sockets-tcp-create-does-not-work-in-app-background-script