I need to create a chrome extension which shows a notification when we get a message from socket io node js server.
How to include socket io in chrome extension? I am not able to get this to working.
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){});
});
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"
]
},
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
You are really really awesome!');
}
io.sockets.on('connection',function(socket){
socket.emit('hello',{text:"node!"});
});
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…