Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
855 views
in Technique[技术] by (71.8m points)

node.js - Chrome Extension Socket io node js

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

"background": {
  "scripts": [
    "socket.io.js",
    "background.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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...