當前位置: 首頁>>代碼示例>>Golang>>正文


Golang Conn.Config方法代碼示例

本文整理匯總了Golang中code/google/com/p/go/net/websocket.Conn.Config方法的典型用法代碼示例。如果您正苦於以下問題:Golang Conn.Config方法的具體用法?Golang Conn.Config怎麽用?Golang Conn.Config使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在code/google/com/p/go/net/websocket.Conn的用法示例。


在下文中一共展示了Conn.Config方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: handleConnection

func handleConnection(conn *net.TCPConn) error {
	defer conn.Close()

	handlerChan <- 1
	defer func() {
		handlerChan <- -1
	}()

	var ws *websocket.Conn

	conn.SetDeadline(time.Now().Add(socksTimeout))
	err := AwaitSocks4aConnect(conn, func(dest string) (*net.TCPAddr, error) {
		// Disable deadline.
		conn.SetDeadline(time.Time{})
		Log("SOCKS request for %s", dest)
		destAddr, err := net.ResolveTCPAddr("tcp", dest)
		if err != nil {
			return nil, err
		}
		wsUrl := url.URL{Scheme: "ws", Host: dest}
		ws, err = websocket.Dial(wsUrl.String(), "", wsUrl.String())
		if err != nil {
			return nil, err
		}
		Log("WebSocket connection to %s", ws.Config().Location.String())
		return destAddr, nil
	})
	if err != nil {
		return err
	}
	defer ws.Close()
	proxy(conn, ws)
	return nil
}
開發者ID:eduardostalinho,項目名稱:flashproxy,代碼行數:34,代碼來源:websocket-client.go

示例2: SocketStreamer

//Handeler for websocket connectionf from client pages. Expects a []int list of feature id's as its first message
//and will submit these tasks and then wait to stream results back
func SocketStreamer(ws *websocket.Conn) {
	fmt.Printf("jsonServer %#v\n", ws.Config())
	//for {
	url := *ws.Config().Location
	id := UniqueId()
	url.Scheme = "http"
	url.Path = "/results/" + id

	var msg []int

	err := websocket.JSON.Receive(ws, &msg)
	if err != nil {
		fmt.Println(err)
		//break
	}
	//TODO:Consider allowing multiple tasks submissions on one channel instead of only waiting for one msg
	fmt.Printf("recv:%#v\n", msg)

	RestultChan := make(chan string, 0)
	WsRegistry[id] = RestultChan
	SubmitTasks(msg, url.String())

	for {
		msg := <-RestultChan
		err = websocket.Message.Send(ws, msg)
		if err != nil {
			fmt.Println(err)
			break
		}
		fmt.Printf("send:%#v\n", msg)
	}

	//TODO: remove ws from registry and cancel outstandign jobs when connetion dies
}
開發者ID:ryanbressler,項目名稱:StreamingRface,代碼行數:36,代碼來源:streamingrface.go

示例3: wsHandler

//wsHandler now used ws.Config as protocol handshake now supported
func wsHandler(ws *websocket.Conn) {
	defer flow.DontPanic()
	defer ws.Close()

	hdr := ws.Request().Header

	// keep track of connected clients for reload broadcasting
	id := hdr.Get("Sec-Websocket-Key")
	wsClients[id] = ws
	defer delete(wsClients, id)

	// the protocol name is used as tag to locate the proper circuit
	//lightbulb: We use the protocol provided by ws, rather than header, as that contains server accepted value
	tag := ws.Config().Protocol[0]

	fmt.Println("WS Protocol Selected:", tag)

	if tag == "" { //no specific protocol, lets opt for 'default' which just echoes (or return with no circuit!)
		tag = "default"
	}

	g := flow.NewCircuit()
	g.AddCircuitry("head", &wsHead{ws: ws})
	g.Add("ws", "WebSocket-"+tag) //the client has negotiated this support
	g.AddCircuitry("tail", &wsTail{ws: ws})
	g.Connect("head.Out", "ws.In", 0)
	g.Connect("ws.Out", "tail.In", 0)
	g.Run()
}
開發者ID:TheDistractor,項目名稱:jeebus,代碼行數:30,代碼來源:http.go

示例4: jsonServer

//handle a websocket connection received from webserver
func jsonServer(ws *websocket.Conn) {
	fmt.Printf("jsonServer %#v\n", ws.Config())
	for {
		var msg T

		// Receive receives a text message serialized T as JSON.
		err := websocket.JSON.Receive(ws, &msg)
		if err != nil {
			fmt.Println(err)
			break
		}
		fmt.Printf("recv:%#v\n", msg)

		// Send send a text message serialized T as JSON.
		msg.Msg = "pong"
		msg.Tps = stackLastTps.Pop().(int)

		// Send to the webclients
		err = websocket.JSON.Send(ws, msg)
		if err != nil {
			fmt.Println(err)
			break
		}
		fmt.Printf("send:%#v\n", msg)
	}
}
開發者ID:luisbebop,項目名稱:socket-server,代碼行數:27,代碼來源:socket-server.go

示例5: WSCompileRunHandler

// WSCompileRunHandler handles the websocket connection for a given compile/run action.
// It handles transcoding Messages to and from JSON format, and handles starting
// and killing processes.
func WSCompileRunHandler(c *websocket.Conn) {
	c.Config()
	in, out := make(chan *Message), make(chan *Message)
	errc := make(chan error, 1)

	// Decode messages from client and send to the in channel.
	go func() {
		dec := json.NewDecoder(c)
		for {
			var m Message
			if err := dec.Decode(&m); err != nil {
				fmt.Printf("error in dec.Decode %s\n", err)
				errc <- err
				return
			}
			in <- &m
		}
	}()

	// Receive messages from the out channel and encode to the client.
	go func() {
		enc := json.NewEncoder(c)
		for m := range out {
			if err := enc.Encode(m); err != nil {
				fmt.Printf("error in enc.Encode %s\n", err)
				errc <- err
				return
			}
		}
	}()

	// Start and kill Processes and handle errors.
	proc := make(map[string]*Process)
	for {
		select {
		case m := <-in:
			switch m.Kind {
			case "run":
				goTest := m.GoTest
				proc[m.Id].Kill()
				if goTest {
					proc[m.Id] = StartTest(m.Id, m.SrcDir, m.RunOpts, nil, out)
				} else {
					proc[m.Id] = StartProcess(m.Id, m.Body, m.BuildOpts, m.RunOpts, nil, out)
				}
			case "kill":
				proc[m.Id].Kill()
			}
		case err := <-errc:
			// A encode or decode has failed; bail.
			log.Println(err)
			// Shut down any running processes.
			for _, p := range proc {
				p.Kill()
			}
			return
		}
	}
}
開發者ID:nilslice,項目名稱:go-play,代碼行數:62,代碼來源:goplay.go

示例6: wsHandler

func wsHandler(ws *websocket.Conn) {
	fmt.Println("NEW WEBSOCKET!")
	fmt.Println(ws.Config())
	fmt.Println(ws.RemoteAddr())
	c := &connection{send: make(chan string, 256), ws: ws}
	go c.writer()
	c.reader()
}
開發者ID:cloudtrends,項目名稱:webrtc-datachannel-polyfill,代碼行數:8,代碼來源:websockserver.go

示例7: wsHandler

func wsHandler(ws *websocket.Conn) {
	c := &client{
		ws:       ws,
		origin:   ws.Config().Origin.String(),
		SendChan: make(chan interface{}, 5),
		RecvChan: make(chan interface{}, 5),
	}
	internalClientConnectChan <- c

	defer func() {
		ClientRmChan <- c
		ClientDisconnectChan <- c
	}()
	go c.send()
	c.recv()
}
開發者ID:huangzhiyong,項目名稱:percona-agent,代碼行數:16,代碼來源:ws_server.go

示例8: socketHandler

func socketHandler(ws *websocket.Conn) {
	loc := ws.Config().Location.String()
	var id string
	websocket.Message.Receive(ws, &id)
	s := socket{ws, make(chan bool), loc, id}

	checkingSocketMap.Lock()
	if _, exist := socketmap[loc]; !exist {
		socketmap[loc] = make(chan socket)
	}
	checkingSocketMap.Unlock()

	go match(s)

	<-s.done
	fmt.Println("[ws] closing connection to " + id + " on channel " + loc)
}
開發者ID:candito,項目名稱:chrypt,代碼行數:17,代碼來源:chat.go

示例9: cmdServer

// cmdServer handles a json string sent from client using websocket.JSON.
func cmdServer(ws *websocket.Conn) {
	fmt.Printf("cmdServer %#v\n", ws.Config())
	defer ws.Close()

	for {
		var msg T
		// Receive receives a text message serialized T as JSON.
		err := websocket.JSON.Receive(ws, &msg)
		if err != nil {
			fmt.Println(err)
			break
		}

		fmt.Printf("recv:%#v\n", msg)
		switch msg.Tag {
		case "init":
			sclangObj, err = sclang.Start(PathToSclang, &WebSocketWriter{ws})
			//sclangObj, err = sclang.Start(PathToSclang, os.Stdout)
		case "start_server":
			err = sclangObj.StartServer()
		case "stop_server":
			err = sclangObj.StopServer()
		case "evaluate":
			err = sclangObj.Evaluate(msg.Value, false)
		case "stop_sound":
			err = sclangObj.StopSound()
		case "toggle_recording":
			err = sclangObj.ToggleRecording()
		case "restart_interpreter":
			err = sclangObj.Restart()
		}
		if err != nil {
			fmt.Println(err)
			break
		}

		// Send send a text message serialized T as JSON.
		err = websocket.JSON.Send(ws, msg)
		if err != nil {
			fmt.Println(err)
			break
		}
		fmt.Printf("send:%#v\n", msg)
	}
}
開發者ID:kn1kn1,項目名稱:go-sclang,代碼行數:46,代碼來源:sclang_example.go

示例10: socketHandler

func socketHandler(ws *websocket.Conn) {
	fmt.Printf("readWriteServer %#v\n", ws.Config())

	clientListEl := clients.PushBack(ws)

	for {
		buf := make([]byte, 100)
		n, err := ws.Read(buf)
		if err != nil {
			fmt.Println(err)
			break
		}
		go handleMessage(buf[:n], ws)
	}

	clients.Remove(clientListEl)

	fmt.Println("readWriteServer finished")
}
開發者ID:jfuchs,項目名稱:text-grid,代碼行數:19,代碼來源:grid.go

示例11: jsonServer

// jsonServer echoes back json string sent from client using websocket.JSON.
func jsonServer(ws *websocket.Conn) {
	fmt.Printf("jsonServer %#v\n", ws.Config())
	for {
		var msg T
		// Receive receives a text message serialized T as JSON.
		err := websocket.JSON.Receive(ws, &msg)
		if err != nil {
			fmt.Println(err)
			break
		}
		fmt.Printf("recv:%#v\n", msg)
		// Send send a text message serialized T as JSON.
		err = websocket.JSON.Send(ws, msg)
		if err != nil {
			fmt.Println(err)
			break
		}
		fmt.Printf("send:%#v\n", msg)
	}
}
開發者ID:shiguanghuxian,項目名稱:go-websocket-sample,代碼行數:21,代碼來源:websocket_echo_sample.go

示例12: linewise

func linewise(down *websocket.Conn, up net.Conn) {
	params, _ := url.ParseQuery(down.Config().Location.RawQuery)
	lineEnding := *LineEnding
	if val := params.Get("lineEnding"); val != "" {
		lineEnding = val
	}
	exit := make(chan bool)
	go func() { io.Copy(down, up); exit <- true }()
	go func() {
		for {
			var msg string
			if rerr := websocket.Message.Receive(down, &msg); rerr != nil {
				break
			}
			if _, werr := up.Write([]byte(msg + lineEnding)); werr != nil {
				break
			}
		}
		exit <- true
	}()
	<-exit
}
開發者ID:stesla,項目名稱:socketeer,代碼行數:22,代碼來源:main.go

示例13: echo

func echo(ws *websocket.Conn) {
	fmt.Printf("echo %#v\n", ws.Config())
	for n := 0; n < 10; n++ {
		msg := "Hello " + string(n+48)
		fmt.Println("Sending to client: " + msg)
		err := websocket.Message.Send(ws, msg)
		if err != nil {
			fmt.Println("Cannot send")
			break
		}

		var reply string
		err = websocket.Message.Receive(ws, &reply)
		if err != nil {
			fmt.Println("Cannot receive")
			break
		}
		fmt.Println("Received back from client: " + reply)
	}

	fmt.Println("echo finished")
}
開發者ID:foamdino,項目名稱:learning-go,代碼行數:22,代碼來源:websocket-example.go

示例14: readWriteServer

// readWriteServer echoes back messages sent from client using Read and Write.
func readWriteServer(ws *websocket.Conn) {
	fmt.Printf("readWriteServer %#v\n", ws.Config())
	for {
		buf := make([]byte, 100)
		// Read at most 100 bytes.  If client sends a message more than
		// 100 bytes, first Read just reads first 100 bytes.
		// Next Read will read next at most 100 bytes.
		n, err := ws.Read(buf)
		if err != nil {
			fmt.Println(err)
			break
		}
		fmt.Printf("recv:%q\n", buf[:n])
		// Write send a message to the client.
		n, err = ws.Write(buf[:n])
		if err != nil {
			fmt.Println(err)
			break
		}
		fmt.Printf("send:%q\n", buf[:n])
	}
	fmt.Println("readWriteServer finished")
}
開發者ID:shiguanghuxian,項目名稱:go-websocket-sample,代碼行數:24,代碼來源:websocket_echo_sample.go

示例15: jsonServer

// jsonServer echoes back json string sent from client using websocket.JSON
func jsonServer(ws *websocket.Conn) {
	fmt.Printf("jsonServer %#v\n", ws.Config())
	for {
		var msg T
		// Receive receives a text message serialized T as JSON
		err := websocket.JSON.Receive(ws, &msg)
		if err != nil {
			fmt.Println(err)
			os.Exit(1)
		}

		fmt.Printf("recv:%#v\n", msg)

		switch msg.Msg {
		case "getpids":
			//p := &pids { GetPids(), }
			msg.Msg = GetPids()
			//out, err := json.Marshal(p)
			//if err != nil {
			//	fmt.Println("Marshal:", err)
			//	os.Exit(1)
			//}
			//fmt.Printf("%v - %v\n", out, p)
			//os.Exit(1)
			//msg.Msg = fmt.Sprintf("%#v", p)
			//msg.Msg = string(p)
		}
		// Send sends a text message serialized T as JSON
		err = websocket.JSON.Send(ws, msg)
		if err != nil {
			fmt.Println(err)
			os.Exit(1)
		}
		//fmt.Printf("send:%#v\n", msg)
	}
}
開發者ID:quiznilo,項目名稱:corona,代碼行數:37,代碼來源:corona.go


注:本文中的code/google/com/p/go/net/websocket.Conn.Config方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。