Chrome扩展在后台页面和内容脚本下使用相同的socket.io连接 [英] chrome extension use the same socket.io connection under background page and content scripts
问题描述
我正在使用socket.io做一个扩展插件,我有一个内容脚本,可以连接到服务器进行实时聊天,但是我也想从后台页面的服务器获取一些信息。它可以单独运行,就像这样在内容脚本中
var socket = io.connect('http:// localhost:3700');
socket.on('dosomething',function(){
console.log(test);
});
在后台页面中
var socket = io.connect('http:// localhost:3700');
socket.on('dosomething',function(){
console.log(test);
});
但有没有什么办法只连接一次,当服务器有类似的东西时
app.js socket.emit('dosomething');
并且会触发具有 socket.on('dosomething')
?
的背景页面或内容脚本。已经尝试使用
chrome.runtime.sendMessage
和 chrome.runtime.onMessage.addListener将由背景页建立的套接字发送到内容脚本
,
但它不起作用:($ / b
$ b
有没有解决方案
非常感谢!
内容脚本中的socket.io客户端,它将在socket.io服务器发送hello请求时将消息发送到后台页面。
内容脚本
var socket = io.connect('http:// localhost:1337');
socket.on 你好,本功能ion(data){
console.log(data.text);
chrome.runtime.sendMessage({msg:socket,text:data.text},function(response){});
});
后台页面:
chrome.runtime.onMessage.addListener(
函数(request,sender,senderResponse){
if(request.msg ===socket) {
console.log(从套接字服务器接收:+ request.text);
}
}
);
服务器端:
var app = require('http')。createServer(handler).listen(1337);
var io = require('socket.io')。listen(app);
函数处理程序(req,res){
console.log(req.url);
res.writeHead(200,{'Content-Type':'text / plain'});
res.end('Hello Node \\\
你真的很棒!');
}
io.sockets.on('connection',function(socket){
socket.emit('hello',{text:node!});
});
背景控制台的屏幕截图: $ b
<希望这对你有帮助。
I'm doing a chrome extension with socket.io , i have a content script that keep connecting to server to make a real-time chat, but i also want to get some information from server in background page. it works well separately like this
in content script
var socket = io.connect('http://localhost:3700');
socket.on('dosomething',function(){
console.log("test");
});
in background page
var socket = io.connect('http://localhost:3700');
socket.on('dosomething',function(){
console.log("test");
});
but is there any way to only connect one time and when server have something like
app.js socket.emit('dosomething');
and will trigger either background page or content script which has socket.on('dosomething')
?
I've tried sending the socket established by background page to content script by using
chrome.runtime.sendMessage
and chrome.runtime.onMessage.addListener
,
but it didn't work :(
Is there any solution?
Thanks a lot!
I tried to write a socket.io client in content script which will send message to background page when socket.io server send "hello" request.
Content Script:
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){});
});
Background page:
chrome.runtime.onMessage.addListener(
function(request,sender,senderResponse){
if(request.msg==="socket"){
console.log("receive from socket server: "+request.text);
}
}
);
Server side:
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!"});
});
Screen shot of background console:
Hope this is helpful for you.
这篇关于Chrome扩展在后台页面和内容脚本下使用相同的socket.io连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!