如何将pubnub密钥与Corona和Javascript一起使用

How to use pubnub keys with Corona and Javascript

本文关键字:Javascript 一起 Corona pubnub 密钥      更新时间:2023-09-26

通过一个简单的基于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

组合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客户门户获取您的API密钥。