我正在用 socket.io 做一个 chrome 扩展,我有一个内容脚本,它不断连接到服务器以进行实时聊天,但我也想从后台页面中的服务器获取一些信息。它像这样单独工作得很好
在内容脚本中
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");
});
但是有没有办法只连接一次,当服务器有类似的东西时
应用程序.js socket.emit('dosomething');
并且将触发具有?的背景页面或内容脚本 socket.on('dosomething')
?
我尝试使用
chrome.runtime.sendMessage
and 将后台页面建立的套接字发送到内容脚本chrome.runtime.onMessage.addListener
,
但它没有用:(
有什么解决办法吗?
非常感谢!