【问题标题】:How to use pubnub keys with Corona and Javascript如何在 Corona 和 Javascript 中使用 pubnub 密钥
【发布时间】:2014-12-23 19:36:39
【问题描述】:

使用简单的基于 Web 的聊天应用程序学习适用于 Corona 的 pubnub。有两个组件,一个是 Corona 中的按钮应用程序,用于发送基本消息和接收基本消息。第二个是一个简单的 html 文件,用 javascript 发送和接收消息。只要我使用名为“演示”的发布和订阅键,该应用程序就可以正常工作。一旦我将密钥更改为我的密钥(由 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

【问题讨论】:

    标签: javascript lua coronasdk pubnub


    【解决方案1】:

    结合 Lua + JavaScript PubNub SDKs

    您需要确保在两个 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 密钥。

    【讨论】:

    • 感谢您的快速回复。我已经按照你在 Lua Corona 和 Javascript 中所说的做了(见下面的代码),但是 javascript 仍然没有接收数据。下面我先发布 Lua/Corona 代码,然后再发布 html/javascript:
    • 我无法发布完整代码,但这是 Corona Lua 代码的相关部分。
    • 您可能遇到了发布与订阅的竞争条件。 在发布之前,您必须订阅 Lua 和 JavaScript 中的两个 SDK。我成功测试了你的 JS 和 Lua 代码。
    • 您好,谢谢。您是否尝试过使用私钥与“演示”密钥?
    • 确实如此! :-) 确保 Lua 和 JS 具有相同的键。同时删除所有 JSON 行,您不需要这样做。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-05
    • 1970-01-01
    相关资源
    最近更新 更多