Node.js - 在您的桌面上运行 JavaScript





0/5 (0投票)
Node.js - 在您的桌面上运行 JavaScript。
Spokes SDK 目前为开发人员提供了三种主要的语言选择:C++、C# 和 JavaScript。每种语言都有其优点和缺点,并且专门用于不同的用途。
JavaScript 通过 REST 与 Spokes 通信,并定期轮询事件。它是一种轻量级的事件驱动编程语言,非常适合用少量代码完成大量工作。然而,它通常用于开发在浏览器中运行的应用程序;JavaScript 依赖于通常在 Web 浏览器(例如 Chrome 的 V8)中运行的虚拟机。
然而,最近围绕一项名为 Node.js 的新技术出现了很多活动。基本上,它是 Chrome 的 V8 JavaScript 引擎,已被剥离出浏览器并在其自身环境中运行;就在您的桌面上。令人兴奋的是,借助 Node.js,您可以使用 JavaScript 编写事件驱动的程序,这些程序可以在您的桌面上运行,而无需浏览器来运行您的代码。
使用 JavaScript 和 Node.js 开始进行一些 Spokes 桌面应用程序开发非常简单明了。那么,让我们来看看如何实现它。
要求
- Node.js - 将为我们的应用程序提供动力的独立 JavaScript 环境。
- Python 2.7.x - 使 jQuery 在浏览器外部正常工作的模块所需的依赖项。
- Spokes SDK - Spokes 工具和运行时环境。(发布时版本为 2.6)
您还需要一个文本编辑器来编写 JavaScript 代码。我使用 Notepad++。
配置
安装好所有内容后,我们需要安装 Node.js 的 jQuery 模块,以使 Spokes JavaScript 库正常工作。
Node.js 安装时,Node 包管理器应该已被添加到您的 PATH 环境变量中,所以请打开命令提示符并运行以下命令:“npm install jquery”。
现在我们需要创建一个新文件夹作为我们的工作目录。在这个工作目录中,我们想复制 Spokes.js JavaScript 库,该库可以在 Spokes SDK 安装目录的 Samples 文件夹中的 RestJsClient 示例中找到。(对我来说,它位于“C:\Program Files\Plantronics\Plantronics SDK\Samples\RestJsClient”)
我们需要对 Spokes.js 文件进行一些小的编辑,以便不仅让 jQuery 为库工作,还公开一些函数供我们外部自定义 JavaScript 文件使用。
我们首先将一些关键对象公开,以便在文件外部使用。将以下行添加到 Spokes.js 的顶部:
module.exports.Spokes = Spokes; module.exports.SpokesCallId = SpokesCallId; module.exports.SpokesContact = SpokesContact;
这将使我们能够利用文件本身之外的 Spokes
、SpokesCallId
和 SpokesContact
对象。
还需要做的另一项更改是告诉 Spokes.js $ 变量等于什么。通常,这通过在我们的基于浏览器的脚本中包含最新的 jQuery 文件来完成,但在 node.js 中,jQuery 的包含/安装方式略有不同。幸运的是,让 jQuery 工作起来非常简单。在我们最后更改之后,添加以下行:
var $ = require('jquery');
并更改
if(typeof jQuery === 'undefined')
to
if(typeof $ === 'undefined')
就是这么简单。
最后,我们需要将所有 alert()
实例替换为 console.log()
。否则,node.js 会抱怨很多,您的程序也将无法工作。您可以在 notepad++ 中按 Ctrl+h 并将“alert("”替换为“console.log("”,或者如果您愿意,可以使用 sed,或者手动完成所有操作。
进行更改后,我们就可以着手处理主 JavaScript 文件了。我将粘贴我之前博客文章中 JavaScript 示例的修改版本,RESTful Programming With Your Voyager Pro UC,因为本文的目的是帮助您开始在桌面上运行 JavaScript 应用程序,而不是向您展示如何开始使用 JavaScript API(如果需要有关如何开始使用的详细信息,请参阅 RESTful Programming 博客文章)。
这段代码应该放在与修改后的 Spokes.js 文件相同的文件夹中。
var SpokesJS = require('./spokes'); //NEW CODE - Node.js automatically knows spokes is a .js file. Required to use the Spokes library. var spokes = null; //spokes session manager (class defined in spokes.js) var plugin_registered = false; var plugin_name = "NodeSpokes"; //------------------------------------------------ // connectToSpokes - attempt to create a new Spokes session manager, register this app, // and get a valid device to work with. Begin polling for events on success. //------------------------------------------------ var connectToSpokes = function() { //attempt to make a new Spokes object with localhost:32001/Spokes as the base url path for REST calls spokes = new SpokesJS.Spokes("https://:32001/Spokes");//Spokes is now an object in the SpokesJS module spokes.Device.deviceList( function(result) { //on success... if(!result.isError) { //on valid device found... if(result.Result[0] !== null) { //attempt to connect to the valid device and start polling for events from it. spokes.Device.attach(result.Result[0].Uid, controlInterface); pollDeviceEvents(); } else { console.log("Error: Device was null on connecting to Spokes. Is there a Plantronics device connected?"); } } else { console.log("Error connecting to Spokes."); } }); }; //------------------------------------------------ // controllerInterface - callback function to that registers this app on successful contact with a valid device. //------------------------------------------------ var controlInterface = function(session) { //if an error popped up in our session or we failed to connect with a device... if(session.isError || !spokes.Device.isAttached) { console.log("Session Registration Error"); } else { registerPlugin(); } }; //------------------------------------------------ // registerPlugin - where the actual dirty work is done for app registration. //------------------------------------------------ var registerPlugin = function() { //only register if we haven't done so already... if(!plugin_registered) { spokes.Plugin.register(plugin_name, function(result) { if(!result.isError) { //successfully registered the plugin. Set status of plugin to active. spokes.Plugin.isActive(plugin_name, true, function(result) { if(!result.isError) { plugin_registered = true; } else { console.log("Error checking if plugin is active: " + result.Err.Description); } }); } else { console.log("Error registering plugin: " + result.Err.Description); } }); } }; //------------------------------------------------ // pollDeviceEvents - regularly ask the Spokes service for new, if any, events to work with. // This is the main program loop where all the program-specific work will be done. //------------------------------------------------ var pollDeviceEvents = function() { setInterval(function() { if(spokes === null || !spokes.Device.isAttached) { return; } //ask for events, if any spokes.Device.events(function(result) { var i; if(result.isError) { console.log("Error polling for events: " + result.Err.Description); } else { //we have one or more events! if(result.Result.length > 0) { i = 0; //iterate over the events while we haven't hit the end of the array of events returned while(i < result.Result.length) { console.log(result.Result[i].Event_Name); i++; //increment counter so we're not stuck in an infinite loop } } } }); }, 2000); //repeat this loop every 2 seconds. }; connectToSpokes(); //Begin the program. Entry point.
将上面的代码保存到……比如说,nodespokes.js。现在,如果一切都已正确安装和配置,您应该可以启动命令提示符,将目录更改为您的工作目录,然后通过键入“C:/program files/nodejs/node” nodespokes.js (假设您安装 node.js 时的默认安装文件夹是 C:/program files/nodejs)来使用 node.js 运行 nodespokes.js 脚本。
打开您的耳机,关闭它,将其移出范围。如果一切正常,您应该会看到一些事件开始显示在命令提示符窗口中。就像这样(当我用手机拨打软电话,而我的耳机同时与计算机和手机配对时)
尽情享受使用 Spokes 在一个奇妙而强大的事件驱动语言中进行开发吧!如果您喜欢冒险,甚至可以看看一些最新的努力,为 node.js 项目提供 GUI 渲染,例如 Appjs。看看人们会将它发展成什么样子将会很有趣。
我已将修改后的 Spokes.js 文件和 nodespokes.js 文件打包在一个 zip 文件中,一旦所有要求都已安装和配置好,您就可以开始使用了。