是否可以从外部应用程序调用nodewebkit中的函数?
例如。我想决定窗口是隐藏还是通过外部应用程序或使用AppleScript显示。
答案 0 :(得分:1)
我不熟悉applescript语言,但可以在具有socket.io
实现的库的语言之间
使用socket.io
您可以在应用程序之间表现,socket.io
就像node.js EventEmitter
(或pubsub)一样,客户端可以发送事件并实时接收这些事件。
对于您的情况,您可以使用node.js
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');
});
并向您的node-webkit应用程序添加socket.io client
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应用程序将控制" show"并且"隐藏"操作
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');
}