github.com/mdaxf/iac@v0.0.0-20240519030858-58a061660378/integration/messagebus/glue/sample/ReadWrite/main.go (about)

     1  /*
     2   *  Glue - Robust Go and Javascript Socket Library
     3   *  Copyright (C) 2015  Roland Singer <roland.singer[at]desertbit.com>
     4   *
     5   *  This program is free software: you can redistribute it and/or modify
     6   *  it under the terms of the GNU General Public License as published by
     7   *  the Free Software Foundation, either version 3 of the License, or
     8   *  (at your option) any later version.
     9   *
    10   *  This program is distributed in the hope that it will be useful,
    11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
    12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13   *  GNU General Public License for more details.
    14   *
    15   *  You should have received a copy of the GNU General Public License
    16   *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17   */
    18  
    19  package main
    20  
    21  import (
    22  	"log"
    23  	"net/http"
    24  
    25  	"github.com/desertbit/glue"
    26  )
    27  
    28  func main() {
    29  	// Set the http file server.
    30  	http.Handle("/", http.StripPrefix("/", http.FileServer(http.Dir("public"))))
    31  	http.Handle("/dist/", http.StripPrefix("/dist/", http.FileServer(http.Dir("../../client/dist"))))
    32  
    33  	// Create a new glue server.
    34  	server := glue.NewServer(glue.Options{
    35  		HTTPListenAddress: ":8080",
    36  	})
    37  
    38  	// Release the glue server on defer.
    39  	// This will block new incoming connections
    40  	// and close all current active sockets.
    41  	defer server.Release()
    42  
    43  	// Set the glue event function to handle new incoming socket connections.
    44  	server.OnNewSocket(onNewSocket)
    45  
    46  	// Run the glue server.
    47  	err := server.Run()
    48  	if err != nil {
    49  		log.Fatalf("Glue Run: %v", err)
    50  	}
    51  }
    52  
    53  func onNewSocket(s *glue.Socket) {
    54  	// Set a function which is triggered as soon as the socket is closed.
    55  	s.OnClose(func() {
    56  		log.Printf("socket closed with remote address: %s", s.RemoteAddr())
    57  	})
    58  
    59  	// Run the read loop in a new goroutine.
    60  	go readLoop(s)
    61  
    62  	// Send a welcome string to the client.
    63  	s.Write("Hello Client")
    64  }
    65  
    66  func readLoop(s *glue.Socket) {
    67  	for {
    68  		// Wait for available data.
    69  		// Optional: pass a timeout duration to read.
    70  		data, err := s.Read()
    71  		if err != nil {
    72  			// Just return and release this goroutine if the socket was closed.
    73  			if err == glue.ErrSocketClosed {
    74  				return
    75  			}
    76  
    77  			log.Printf("read error: %v", err)
    78  			continue
    79  		}
    80  
    81  		// Echo the received data back to the client.
    82  		s.Write(data)
    83  	}
    84  }