<file_sep>package main

import (
	"net"
		"fmt"
	"sync"
	"time"
)

type NetNod struct {
	recvCon *net.UDPConn
	broadCon *net.UDPConn
	recvChan chan []byte
	broadChan chan []byte
	sync.RWMutex
}
var netNod *NetNod = &NetNod{
	recvChan:make(chan []byte,1024),
	broadChan:make(chan []byte,1024),
}
func publish(msg[]byte){

	netNod.broadChan<-msg

}
func StartBroadNode(port int){


	//初始化发送端口
	broadAddr := &net.UDPAddr{
		IP:net.IPv4(192,168,0,255),
		Port:port,
	}


	broadCon ,err:=  net.DialUDP("udp",nil,broadAddr)
	defer  broadCon.Close()
	if err!=nil{
		fmt.Println(err)
		panic(err)
	}
	netNod.broadCon = broadCon



		for   {
			select {
			case d := <-netNod.broadChan:

				broadCon.Write(d)

			}
		}


}

func StartRecvNode(port int){
	recvAddr := &net.UDPAddr{
		IP:net.IPv4zero,
		Port:port,
	}
	recvCon ,err := net.ListenUDP("udp",recvAddr)
	defer  recvCon.Close()
	if err!=nil{
		fmt.Println(err)
		panic(err)
	}
	netNod.recvCon = recvCon
	//初始化变量


	for{
		var buf [250]byte

		n, raddr, err := recvCon.ReadFromUDP(buf[0:])

		if err != nil {
			fmt.Println(err.Error())
			return;
		}
		if n>1 && raddr!=nil{

		}

		netNod.recvChan <- buf[0:n]
		//判断这个数据是不是自己的,是就处理,不是就抛弃
		//fmt.Println("msg from ",raddr," is ", string(buf[0:n]))
	}
}
func main()  {
	go StartRecvNode(3000)
	go StartBroadNode(3000)
	t1 := time.NewTicker(5*time.Second)
	for{
		select {
		case  d := <-netNod.recvChan:
			fmt.Println("<=",string(d))
			break
		case <-t1.C:
			publish([]byte(time.Now().String()))
		}
	}

}
<file_sep>package ctrl

import (
	"net/http"
	"fmt"
	"math/rand"
	"../util"
	"../service"
	"../model"
)

func UserLogin(writer http.ResponseWriter,
	request *http.Request) {
	//数据库操作
	//逻辑处理
	//restapi json/xml返回
	//1.获取前端传递的参数
	//mobile,passwd
	//解析参数
	//如何获得参数
	//解析参数
	request.ParseForm()

	mobile := request.PostForm.Get("mobile")
	passwd := request.PostForm.Get("passwd")

	loginok := false
	if(mobile=="18600000000" && passwd=="<PASSWORD>"){
		loginok = true
	}
	if(loginok){
		//{"id":1,"token":"xx"}
		data := make(map[string]interface{})
		data["id"]=1
		data["token"]="test"
		util.RespOk(writer,data,"")
	}else{
		util.RespFail(writer,"密码不正确")

	}
	//go get github.com/go-xorm/xorm
	//go get github.com/go-sql-driver/mysql
	//返回json ok

	//如何返回JSON

	//io.WriteString(writer,request.PostForm.Get("mobile"))
}
var userService service.UserService
func UserRegister(writer http.ResponseWriter,
	request *http.Request) {

	request.ParseForm()
	//
	mobile := request.PostForm.Get("mobile")
	//
	plainpwd := request.PostForm.Get("passwd")
	nickname := fmt.Sprintf("user%06d",rand.Int31())
	avatar :=""
	sex := model.SEX_UNKNOW

	user,err := userService.Register(mobile, plainpwd,nickname,avatar,sex)
	if err!=nil{
		util.RespFail(writer,err.Error())
	}else{
		util.RespOk(writer,user,"")

	}

}
<file_sep>package util

import (
	"net/http"
	"encoding/json"
	"log"
)

type H struct {
	Code int `json:"code"`
	Msg  string `json:"msg"`
	Data interface{} `json:"data,omitempty"`
}
//
func RespFail(w http.ResponseWriter,msg string){
	Resp(w,-1,nil,msg)
}
func RespOk(w http.ResponseWriter,data interface{},msg string){
	Resp(w,0,data,msg)
}
func Resp(w http.ResponseWriter,code int,data interface{},msg string)  {

	w.Header().Set("Content-Type","application/json")
	//设置200状态
	w.WriteHeader(http.StatusOK)
	//输出
	//定义一个结构体
	h := H{
		Code:code,
		Msg:msg,
		Data:data,
	}
	//将结构体转化成JSOn字符串
	ret,err := json.Marshal(h)
	if err!=nil{
		log.Println(err.Error())
	}
	//输出
	w.Write(ret)
}
