GithubHelp home page GithubHelp logo

lens's Introduction

[Deprecated]

use https://github.com/shynome/lens2

一个简易消息传递服务

原始目标是 webrtc 的信令服务器, 但后面想了想, 其实可以做成简易的消息传递服务, 于是提取出来了作为单独的一个库/标准.

https://github.com/rabbitmq/amqp091-go 的简化版/弱化版

架构

  • 任务处理方
    • 通过 EventSource 监听进来的请求并处理
    • 任务处理完成后通过 http.MethodDelete 返回处理结果
  • 调用方
    • 通过 http.MethodPost 发送二进制消息到对应主题

注: 可以对二进制消息进行加密防止服务器对消息进行窥探

使用

可以参考 signaler_test.go 文件中的 TestSignaler 函数

启动信令服务器

package main

import (
	"net/http"

	"github.com/shynome/lens"
)

func main() {
	s := lens.New(nil)
  	http.Handle("/signaler", s)
	http.ListenAndServe(":7070", nil)
}

监听特定主题的消息并处理

package main

import (
	"github.com/donovanhide/eventsource"
	"github.com/lainio/err2/try"
	"github.com/shynome/lens/sdk"
)

func main() {
  /*
  a:b 必不可少, 每个用户都有一个属于自己的信令服务
  t=7 必不可少, 监听/呼叫/处理对应主题的消息
  */
	var endpoint = "http://a:[email protected]:7070/signaler?t=7"
	sdk := sdk.New(endpoint)
	stream := try.To1(
		eventsource.Subscribe(endpoint, ""))
	for ev := range stream.Events {
    // 直接返回输入
		go sdk.Dial(ev.Id(), []byte(ev.Data()))
	}
}

使用

package main

import (
	"bytes"
	"fmt"

	"github.com/lainio/err2/try"
	"github.com/shynome/lens/sdk"
)

func main() {
	sdk := sdk.New("http://a:[email protected]:7070/signaler?t=7")
	var input = []byte("hello")
	rbody := try.To1(
		sdk.Call(input))
	if !bytes.Equal(rbody, input) {
		try.To(fmt.Errorf("expect %s, got %s", input, rbody))
	}
}

lens's People

Contributors

shynome 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.