github.com/mdaxf/iac@v0.0.0-20240519030858-58a061660378/integration/messagebus/glue/sample/OnlyWrite/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  func onNewSocket(s *glue.Socket) {
    53  	// Set a function which is triggered as soon as the socket is closed.
    54  	s.OnClose(func() {
    55  		log.Printf("socket closed with remote address: %s", s.RemoteAddr())
    56  	})
    57  
    58  	// Discard all reads.
    59  	// If received data is not discarded, then the read buffer will block as soon
    60  	// as it is full, which will also block the keep-alive mechanism of the socket.
    61  	// The result would be a closed socket...
    62  	s.DiscardRead()
    63  
    64  	// Send a welcome string to the client.
    65  	s.Write("Hello Client")
    66  }