Chrome扩展套接字io节点js [英] Chrome Extension Socket io node js

查看:62
本文介绍了Chrome扩展套接字io节点js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建一个chrome扩展名,以便在我们从套接字io节点js服务器收到消息时显示通知.

I need to create a chrome extension which shows a notification when we get a message from socket io node js server.

如何在Chrome扩展程序中包含socket io?我无法使它正常工作.

How to include socket io in chrome extension? I am not able to get this to working.

Content.js:-未捕获的ReferenceError:未定义io

Content.js:- Uncaught ReferenceError: io is not defined

var socket = io.connect('http://localhost:1337');
socket.on("hello",function(data){
    console.log(data.text);
    chrome.runtime.sendMessage({msg:"socket",text:data.text},function(response){});
});

清单:-这不是导入套接字io 无法从以下位置加载扩展程序:无法加载后台脚本' http://localhost:1337/socket.io/socket.io.js '.

Manifest:- This is not importing socket io Failed to load extension from: Could not load background script 'http://localhost:1337/socket.io/socket.io.js'.

    "background": {
    "scripts": [
        "http://localhost:1337/socket.io/socket.io.js",
        "background.js"
    ]
},

节点server.js

node server.js

var app = require('http').createServer(handler).listen(1337);
var io = require('socket.io').listen(app);

function handler(req,res){
    console.log(req.url);
    res.writeHead(200, {'Content-Type':'text/plain'});
    res.end('Hello Node\n You are really really awesome!');
}

io.sockets.on('connection',function(socket){
    socket.emit('hello',{text:"node!"});
});

推荐答案

由于您只需要socket.io-client,所以您应该这样做:

Since you only need the socket.io-client, this is what you should be doing:

"background": {
  "scripts": [
    "socket.io.js",
    "background.js"
  ]
},

从此处下载并添加socket.io.js文件:https://raw.githubusercontent.com/Automattic/socket.io-client/1.3.5/socket.io.js

Download and add the socket.io.js file from here: https://raw.githubusercontent.com/Automattic/socket.io-client/1.3.5/socket.io.js

这篇关于Chrome扩展套接字io节点js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆