Skip to content

Instantly share code, notes, and snippets.

@darrenjs
Forked from Mdrbhatti/publish.py
Created August 8, 2018 06:19
Show Gist options
  • Save darrenjs/860cfe0f0a5eeaf5b8dc01902a40717b to your computer and use it in GitHub Desktop.
Save darrenjs/860cfe0f0a5eeaf5b8dc01902a40717b to your computer and use it in GitHub Desktop.
Protocol error with wampcc + autobahn python publish
###############################################################################
#
# The MIT License (MIT)
#
# Copyright (c) Crossbar.io Technologies GmbH
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
###############################################################################
import asyncio
from os import environ
from autobahn.asyncio.wamp import ApplicationSession, ApplicationRunner
from autobahn.wamp.types import PublishOptions
class Component(ApplicationSession):
"""
An application component that publishes an event every second.
"""
async def onJoin(self, details):
counter = 0
while True:
print("publish: com.myapp.topic1", counter)
self.publish(u'com.myapp.topic1', counter)
counter += 1
await asyncio.sleep(1)
if __name__ == '__main__':
import six
url = environ.get("AUTOBAHN_DEMO_ROUTER", u"ws://127.0.0.1:8080/ws")
if six.PY2 and type(url) == six.binary_type:
url = url.decode('utf8')
realm = u"default_realm"
runner = ApplicationRunner(url, realm)
runner.run(Component)
/*
* Copyright (c) 2017 Darren Smith
*
* wampcc is free software; you can redistribute it and/or modify
* it under the terms of the MIT license. See LICENSE for details.
*/
#include "wampcc/wampcc.h"
#include <iostream>
#include <random>
#ifndef _WIN32
#include <unistd.h>
#else
#include <process.h>
#endif
int main(int argc, char** argv)
{
try {
std::string port = "8080";
std::promise<void> can_exit;
/* Create the wampcc logger & kernel. */
//auto logger = wampcc::logger::console();
auto logger = wampcc::logger::stream(wampcc::logger::lockable_cout,
wampcc::logger::levels_upto(wampcc::logger::eDebug),
true);
wampcc::kernel the_kernel;
logger.write(wampcc::logger::eInfo, wampcc::package_string(), __FILE__, __LINE__);
/* Create an embedded wamp router. */
wampcc::wamp_router router(&the_kernel);
wampcc::auth_provider auth = wampcc::auth_provider::no_auth_required();
wampcc::wamp_router::listen_options listen_opts;
listen_opts.service = port;
auto fut = router.listen(auth, listen_opts);
if (fut.wait_for(std::chrono::milliseconds(250)) !=
std::future_status::ready)
throw std::runtime_error("timeout during router listen");
if (auto ec = fut.get())
throw std::runtime_error("listen failed: err " +
std::to_string(ec.os_value()) + ", " +
ec.message());
logger.write(wampcc::logger::eInfo,
"socket listening on " + port,
__FILE__, __LINE__);
/* Provide several RPCs */
router.callable("default_realm", "greeting",
[](wampcc::wamp_router&, wampcc::wamp_session& caller, wampcc::call_info info) {
caller.result(info.request_id, {"hello"});
});
router.callable("default_realm", "pid",
[](wampcc::wamp_router&, wampcc::wamp_session& caller, wampcc::call_info info) {
caller.result(info.request_id, {getpid()});
});
/* Demonstrate sending an error as the RPC result. */
router.callable("default_realm", "stop",
[&can_exit](wampcc::wamp_router&, wampcc::wamp_session&, wampcc::call_info) {
can_exit.set_value();
});
/* Suspend main thread */
can_exit.get_future().wait();
} catch (const std::exception& e) {
std::cout << e.what() << std::endl;
return 1;
}
}
###############################################################################
#
# The MIT License (MIT)
#
# Copyright (c) Crossbar.io Technologies GmbH
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
###############################################################################
import asyncio
from os import environ
from autobahn.asyncio.wamp import ApplicationSession, ApplicationRunner
class Component(ApplicationSession):
"""
An application component that subscribes and receives events, and
stop after having received 5 events.
"""
async def onJoin(self, details):
self.received = 0
def on_event(i):
print("Got event: {}".format(i))
self.received += 1
if self.received > 5:
self.leave()
await self.subscribe(on_event, u'com.myapp.topic1')
def onDisconnect(self):
asyncio.get_event_loop().stop()
if __name__ == '__main__':
import six
url = environ.get("AUTOBAHN_DEMO_ROUTER", u"ws://127.0.0.1:8080/ws")
if six.PY2 and type(url) == six.binary_type:
url = url.decode('utf8')
realm = u"default_realm"
runner = ApplicationRunner(url, realm)
runner.run(Component)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment