connect

package
v0.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 30, 2021 License: MIT Imports: 6 Imported by: 0

README

connect

Package connect switchs protocol from HTTP to TCP connection using CONNECT HTTP method.

Get started

Install
go get github.com/hslam/handler
Import
import "github.com/hslam/handler"
Usage
Example

server

package main

import (
	"bufio"
	"github.com/hslam/handler/connect"
	"github.com/hslam/mux"
	"io"
	"log"
	"net"
	"net/http"
	"strings"
)

func main() {
	m := mux.New()
	m.HandleFunc("/connect", func(w http.ResponseWriter, r *http.Request) {
		if conn, err := connect.Connect(w, r); err == nil {
			ServeConn(conn)
		}
	}).CONNECT()
	log.Fatal(http.ListenAndServe(":8080", m))
}

func ServeConn(conn net.Conn) {
	reader := bufio.NewReader(conn)
	for {
		message, err := reader.ReadString('\n')
		if err != nil || err == io.EOF {
			break
		}
		conn.Write([]byte(strings.ToUpper(string(message))))
	}
}

Client

package main

import (
	"bufio"
	"fmt"
	"github.com/hslam/handler/connect"
	"io"
	"time"
)

func main() {
	//conn,err:=connect.DialHTTP("http://localhost:8080/connect")
	conn, err := connect.DialHTTPPath("localhost:8080", "/connect")
	defer conn.Close()
	if err != nil {
		panic(err)
	}
	reader := bufio.NewReader(conn)
	for i := 0; i < 3; i++ {
		conn.Write([]byte("Hello mux\n"))
		message, err := reader.ReadString('\n')
		if err != nil || err == io.EOF {
			break
		}
		fmt.Print(message)
		time.Sleep(time.Second)
	}
}

Output

HELLO MUX
HELLO MUX
HELLO MUX
License

This package is licensed under a MIT license (Copyright (c) 2019 Meng Huang)

Author

connect was written by Meng Huang.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Connect

func Connect(w http.ResponseWriter, r *http.Request) (net.Conn, error)

func DialHTTP

func DialHTTP(u string) (net.Conn, error)

func DialHTTPPath

func DialHTTPPath(address, path string) (net.Conn, error)

Types

This section is empty.

Directories

Path Synopsis
client command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL