Code Monkey home page Code Monkey logo

libuvnsq's Introduction

uvnsq

Async C++ client library for NSQ

This project has done the compiler optimization to the original project UVNSQ, has added the example

This project is based on uvnsq

BUILD

#linux or mac
./build.sh

#windows
Not yet realized

#example
mkdir build 
cd build 
cmake .. && make -j4

Example

PUB:

#include <iostream>

#include <unistd.h>
#include <thread>

#include <uvnsq/NsqProducer.h>


int main()
{
    uv::LogWriter::Instance()->setLevel(uv::LogWriter::Warn);

    uv::EventLoop loop;
    uv::SocketAddr addr("127.0.0.1",4150);

    nsq::NsqProducer npub(&loop,addr);
    std::string topic = "123456";
    std::string bady= "hello nsq";

   
    while(1) {
        npub.pub(topic,bady);
        std::cout << "pub: " << bady << std::endl;
        loop.runNoWait();
        uv_sleep(1000);
    }
     
    return 0;
}


SUB:

#include <iostream>
#include <uvnsq/NsqConsumer.h>


int main()
{
    uv::LogWriter::Instance()->setLevel(uv::LogWriter::Warn);

    uv::EventLoop loop;
    std::string topic = "123456";
    std::string channal = "ch1";

    nsq::NsqConsumer nsub(&loop,topic,channal);
    uv::SocketAddr addr("127.0.0.1",4150);
  
    nsub.setNsqd(addr);

    std::function<void(std::string&)> fun = [](std::string & msg){
        std::cout << "response msg: " << msg <<std::endl;
    };
    nsub.setOnNsqResp(fun);

    std::function<void(nsq::NsqMessage&)> funm = [](nsq::NsqMessage&msg){
        std::cout << "sub: " << msg.MsgBody() <<std::endl;
    };
    nsub.setOnNsqMessage(funm);

    nsub.sub(topic,channal);
    nsub.start();


    while(1)
    {
        int num = loop.runNoWait();
        if(num > 0) {
            uv_sleep(1000);
            //std::cout << " runNoWait ....num: "<< num <<std::endl;
        }else {
            uv_sleep(1000);
            //std::cout << " runNoWait ....num: "<< num <<std::endl;
        }
    }

    return 0;
}

libuvnsq's People

Contributors

mt21625457 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.