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





0/5 (0投票)
Node.js - 在您的桌面上运行 Spokes 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 文件。我将粘贴我上一篇博文《使用 Voyager Pro UC 进行 RESTful 编程》中的 JavaScript 示例的编辑版本,因为本文的目的是帮助您开始在桌面上运行 JavaScript 应用程序,而不是向您展示如何开始使用 JavaScript API(如果需要有关如何开始的详细信息,请参阅 RESTful 编程博文)。
此代码应放在与修改后的 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 文件,您可以在所有需求都已安装和配置好后使用它。
本文由 Brandon Haston 撰写。Brandon Haston 是一名软件开发人员,自 2012 年 7 月起一直在 Plantronics 创造令人兴奋的产品。Brandon 于 2001 年首次进入软件行业,当时他是一名自由 PHP 讲师。他主要在视频游戏行业工作,曾担任 IsoTX 和 Chronic Logic 的程序员,然后共同创立了一家独立游戏工作室 Ethereal Muse Studios。Brandon 目前居住在加利福尼亚州圣克鲁兹。