【问题标题】:Access Node-Webkit App from other Application从其他应用程序访问 Node-Webkit 应用程序
【发布时间】:2015-06-22 23:55:06
【问题描述】:

是否可以从外部应用程序调用 nodewebkit 中的函数?

例如。我想决定窗口是隐藏还是通过外部应用程序或applescript显示。

【问题讨论】:

  • 初次执行还是运行后?
  • 运行后。我想让它在后台运行,并在我需要时使其处于活动状态。
  • 也许您可以使用 socket.io 服务器作为应用程序(您的 node-webkit 应用程序和外部应用程序)之间的服务总线

标签: node.js node-webkit


【解决方案1】:

我不熟悉 applescript 语言,但可以在为 socket.io 实现库的语言之间使用

使用socket.io,您可以在应用程序之间进行操作,socket.io 就像一个 node.js EventEmitter(或 pubsub),客户端可以发送事件并实时订阅这些事件。

对于您的情况,您可以使用 node.js 创建一个 socket.io 服务器

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);

io.on('connection', function(socket){

  // Listens the 'control-hide' event
  socket.on('control-hide', function () {
      // Emit for all connected sockets, the node-webkit app knows hot to handle it
      io.emit('hide');
  });

  // Listens the 'control-show' event
  socket.on('control-show', function () {
      // Emit for all connected sockets, the node-webkit app knows hot to handle it
      io.emit('show');
  });
});

http.listen(3000, function(){
  console.log('listening on *:3000');
});

并将socket.io client 添加到您的 node-webkit 应用程序

var socket = require('socket.io-client')('http://localhost:3000'); // I will assume that the server is in the same machine

socket.on('connect', function(){
  console.log('connected');
});

// Listens the 'hide' event
socket.on('hide', function(){
  // hide window
});

// Listens the 'show' event
socket.on('show', function(){
  // show window
});

对于这个例子,我假设另一个 javascript 应用程序将控制“显示”和“隐藏”操作

var socket = require('socket.io-client')('http://localhost:3000'); // I will assume that the server is in the same machine

socket.on('connect', function(){
  console.log('connected');
});

// sends a 'control-show' event to the server
function show() {
  socket.emit('control-show');
}

// sends a 'control-hide' event to the server
function hide() {
  socket.emit('control-hide');
}

【讨论】:

    猜你喜欢
    • 2013-10-01
    • 1970-01-01
    • 2012-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多