【问题标题】:Why I am not receiving anything from the JS client (as Chrome Extension)?为什么我没有从 JS 客户端(作为 Chrome 扩展程序)收到任何东西?
【发布时间】:2019-01-30 16:48:41
【问题描述】:

简介

我正在学习 JS(在搞砸了一点 Java 之后),我偶然发现了 javascript 中的套接字。

问题

我创建了一个正在侦听的 python 服务器,然后我将 JS 扩展加载到 Chrome,但服务器没有收到任何消息。我在哪里做错了或我错过了什么?

ma​​nifest.Json(也许它是相关的,因为我不能将这个 .js 作为“背景”)

 {
"manifest_version": 2,
"name": "Server-Test-Ext",
"version": "0.1",

"content_scripts": [
{
    "matches": ["<all_urls>"],
    "js": ["socket.js", "client.js"]


}]


}

client.js(来自 Socket.io 网站的示例)

var socket = new io.Socket();

socket.connect('https://localhost:8080');   

socket.on('connect', function(){
    // connected!
  });
  socket.on('message', function(msg){
      // message coming
  });
socket.send('Hello world!');

server.py

import socket

HOST = '127.0.0.1'
PORT = 8080

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    s.bind((HOST, PORT))
    s.listen()
    conn, addr = s.accept()
    with conn: 
        print('Connected by', addr)
        while True:
            data = conn.recv(1024)
            if not data:
                break
            conn.sendall(data)

服务器成功创建了tcp(你可以通过输入CMD netstat -an看到)

注意事项:

- I am using Visual Studio Code (cool text editor, still learning it)
- I am new to the subject (self-taught)

我的期望是在 ext 之前运行的 python 服务器上接收消息。很明显。

感谢您对这个问题感兴趣。

【问题讨论】:

  • var socket = new io.Socket();你不应该在这里指定目标ip和端口吗?
  • 只有在 Chrome 上查找时才会出现错误。如未定义。

标签: javascript python sockets google-chrome-extension visual-studio-code


【解决方案1】:

您需要为您的 manifest.js 声明套接字权限。

详情: https://developer.chrome.com/apps/manifest/sockets

【讨论】:

    猜你喜欢
    • 2015-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-05
    • 1970-01-01
    相关资源
    最近更新 更多