Why chrome.sockets.tcp.create() does not work in app background script?

北慕城南 提交于 2019-12-12 11:36:50

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!