javascript - 如何通过 Corona 和 Javascript 使用 pubnub key

标签 javascript lua coronasdk pubnub

通过一个简单的基于网络的聊天应用程序学习 pubnub 应用于 Corona 的情况。有两个组件,一个是 Corona 中的按钮应用程序,用于发送基本消息和接收基本消息。第二个是一个简单的 html 文件,使用 javascript 发送和接收消息。只要我使用称为“演示”的发布和订阅键,该应用程序就可以正常工作。一旦我将 key 更改为我的 key (由 pubnub 提供),就不会收到消息。我首先粘贴 html/javascript 代码,然后粘贴 Corona。我不明白当 html 没有定义任何键时它是如何订阅的,并且不太确定如何将我的键合并到 html 中。希望这是一个非常简单的问题,并且有人愿意简单地修改 JavaScript 以使我能够使用自己的 key 。首先是 html:

<html>
Enter Chat and press enter
<div><input id=input placeholder=you-chat-here /></div>

Chat Output
<div id=box></div>

<script src=http://cdn.pubnub.com/pubnub.min.js></script>
<script>(function(){
  var box = PUBNUB.$('box'), input = PUBNUB.$('input'), channel = 'z';
  PUBNUB.subscribe({
  channel : channel,
  callback : function(text) {
  var tst = JSON.stringify(text);
  var obj = eval ("(" + (''+tst).replace( /[<>]/g, '' ) + ")");
  box.innerHTML = obj['msgtext'] + '<br>' + box.innerHTML; }
  });
  PUBNUB.bind( 'keyup', input, function(e) {
  (e.keyCode || e.charCode) === 13 &&
  PUBNUB.publish({
  channel : channel,
  message : { "msgtext": input.value },
  x : (input.value='')
  })
})
})()</script>
</html> 

这是电晕代码:

-- 
-- Abstract: Button Events sample app, showing different button properties and handlers.
-- (Also demonstrates the use of external libraries.)
-- 
-- Version: 1.1
-- 
-- Sample code is MIT licensed, see http://www.coronalabs.com/links/code/license
-- Copyright (C) 2010 Corona Labs Inc. All Rights Reserved.

-- This example shows you how to create buttons in various ways by using the widget library.
-- The project folder contains additional button graphics in various colors.
--
-- Supports Graphics 2.0

-- Require the widget library
require "pubnub"

local widget = require( "widget" )

local background = display.newImage("carbonfiber.jpg", true) -- flag overrides large image downscaling
background.x = display.contentWidth / 2
background.y = display.contentHeight / 2

local roundedRect = display.newRoundedRect( 10, 50, 300, 40, 8 )
roundedRect.anchorX, roundedRect.anchorY = 0.0, 0.0     -- simulate TopLeft alignment
roundedRect:setFillColor( 0/255, 0/255, 0/255, 170/255 )

local t = display.newText( "waiting for button event...", 0, 0, native.systemFont, 18 )
t.x, t.y = display.contentCenterX, 70
-------------------------------------------------------------------------------
-- Create 5 buttons, using different optional attributes
-------------------------------------------------------------------------------

multiplayer = pubnub.new({
    publish_key   = "demo",
    subscribe_key = "demo",
    secret_key    = nil,
    ssl           = nil,
    origin        = "pubsub.pubnub.com"
})

multiplayer:subscribe({
    channel  = "z",
    callback = function(message)
        t.text = "I RECEIVED: " .. message.msgtext
    end,
    errorback = function()
        print("Oh no!!! Dropped 3G Conection!")
    end
})

-- These are the functions triggered by the buttons

local button1Press = function( event )
    t.text = "publish"

    multiplayer:publish({
        channel = "z",
        message = { msgtext = "sent from corona!" }
    })

end

local button1Release = function( event )
    t.text = "Button 1 released"
end


local buttonHandler = function( event )
    t.text = "id = " .. event.target.id .. ", phase = " .. event.phase
end


-- This button has individual press and release functions
-- (The label font defaults to native.systemFontBold if no font is specified)

local button1 = widget.newButton
{
    defaultFile = "buttonRed.png",
    overFile = "buttonRedOver.png",
    label = "PUBLISH",
    emboss = true,
    onPress = button1Press
}


-- These other four buttons share a single event handler function, identifying   
 themselves by "id"
-- Note that if a general "onEvent" handler is assigned, it overrides the "onPress" 
   and "onRelease" handling

-- Also, some label fonts may appear vertically offset in the Simulator, but not on 
   device, due to
-- different device font rendering. The button object has an optional "offset" 
   property for minor
-- vertical adjustment to the label position, if necessary (example: offset = -2)

local button2 = widget.newButton
{
    id = "button2",
    defaultFile = "buttonYellow.png",
    overFile = "buttonYellowOver.png",
    label = "Button 2 Label",
    labelColor = 
    { 
        default = { 51, 51, 51, 255 },
    },
    font = native.systemFont,
    fontSize = 22,
    emboss = true,
    onEvent = buttonHandler,
}

local button3 = widget.newButton
{
    id = "button3",
    defaultFile = "buttonGray.png",
    overFile = "buttonBlue.png",
    label = "Button 3 Label",
    font = native.systemFont,
    fontSize = 28,
    emboss = true,
    onEvent = buttonHandler,
}

local buttonSmall = widget.newButton
{
    id = "smallBtn",
    defaultFile = "buttonBlueSmall.png",
    overFile = "buttonBlueSmallOver.png",
    label = " I'm Small",
    fontSize = 12,
    emboss = true,
    onEvent = buttonHandler,
}

-- Of course, buttons don't always have labels
local buttonArrow = widget.newButton
{
    id = "arrow",
    defaultFile = "buttonArrow.png",
    overFile = "buttonArrowOver.png",
    onEvent = buttonHandler,
}

button1.x = 160; button1.y = 160
button2.x = 160; button2.y = 240
button3.x = 160; button3.y = 320
buttonSmall.x = 85; buttonSmall.y = 400
buttonArrow.x = 250; buttonArrow.y = 400

最佳答案

结合 Lua + JavaScript PubNub SDK

您需要确保您的 API key 已添加到两个 SDK 初始化中。 您可以使用以下示例在 JavaScript 和 Lua 中轻松完成此操作。

PubNub JavaScript

var PUBNUB = PUBNUB({ subscribe_key : 'demo', publish_key : 'demo' });

PubNub Lua Corona

multiplayer = pubnub.new({ publish_key = "demo", subscribe_key = "demo" })

通过 PubNub Customer Portal 获取您的 API key .

关于javascript - 如何通过 Corona 和 Javascript 使用 pubnub key ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26767427/

相关文章:

javascript - 如何在 RequireJS 模块中进行异步初始化

mysql - Garrysmod SQL 包装器

lua - 错误: attempt to index nil with 'Agility'

android - 我可以在 Corona 中使用麦克风吗?

events - Corona SDK event.target 触摸/点击

javascript - 如何在 nvd3 简单折线图中默认禁用某些流?

在任何情况下都运行代码的Javascript switch case?

javascript - 水平拖动阻止了页面默认的垂直滚动

lua - torch7 的意外行为

android - Corona SDK 如何为 Android 构建