Chrome扩展套接字io节点js
我需要创build一个chrome扩展,当我们从套接字io节点js服务器收到消息时显示通知。
如何在chrome扩展中包含socket io? 我无法得到这个工作。
Content.js: – 未捕获的ReferenceError:io未定义
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 ”。
"background": { "scripts": [ "http://localhost:1337/socket.io/socket.io.js", "background.js" ] },
节点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,所以你应该这样做:
"background": { "scripts": [ "socket.io.js", "background.js" ] },
从这里下载并添加socket.io.js文件: https : //raw.githubusercontent.com/Automattic/socket.io-client/1.3.5/socket.io.js