如何使用Corona和Javascript的pubnub键

时间:2014-11-05 21:24:59

标签: javascript lua corona pubnub

使用简单的基于Web的聊天应用程序学习应用于Corona的pubnub。有两个组件,一个是Corona中的按钮应用程序,它发送基本消息并接收基本消息。第二个是一个简单的html文件,带有javascript来发送和接收消息。只要我使用名为" demo"的发布和订阅密钥。该应用程序工作正常。一旦我将密钥更改为我的密钥(由pubnub提供),就不会收到消息。我首先粘贴html / javascript代码,然后粘贴Corona第二代。我不明白html如何订阅它没有定义任何键,也不太确定如何将我的键合并到html中。希望这是一个非常简单的问题,有人可能愿意简单地修改javascript以使我能够使用自己的密钥。首先是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

1 个答案:

答案 0 :(得分:0)

结合Lua + JavaScript PubNub SDK

您需要确保在两个SDK初始化中添加了API密钥。 您可以使用以下示例在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密钥。