func (pkt *ConnectPacket) WriteTo(w io.Writer, version byte) (len int, err error) {

	var d int
	len, err = pkt.Header().WriteTo(w)
	if err != nil {
		return
	}

	d, err = writeString(w, pkt.Protocol)
	len += d
	if err != nil {
		return
	}

	d, err = w.Write([]byte{pkt.Version})
	len += d
	if err != nil {
		return
	}

	var flag byte
	if pkt.CleanSession {
		flag |= 0x02
	}
	if pkt.Will != nil {
		flag |= 0x04
		flag |= pkt.Will.QoS << 3
		if pkt.Will.Retain {
			flag |= 0x20
		}
	}
	if pkt.Auth != nil {
		flag |= 0x40 // Username
		flag |= 0x80 // PAssword
	}

	d, err = w.Write([]byte{flag})
	len += d
	if err != nil {
		return
	}

	d, err = writeInt(w, pkt.KeepAliveTimer)
	len += d
	if err != nil {
		return
	}

	d, err = writeString(w, pkt.ClientID)
	len += d
	if err != nil {
		return
	}

	if pkt.Will != nil {
		d, err = writeString(w, pkt.Will.Topic)
		len += d
		if err != nil {
			return
		}
		d, err = writeBytes(w, pkt.Will.Data)
		len += d
		if err != nil {
			return
		}
	}

	if pkt.Auth != nil {
		d, err = writeString(w, pkt.Auth.Username)
		len += d
		if err != nil {
			return
		}
		d, err = writeString(w, pkt.Auth.Password)
		len += d
		if err != nil {
			return
		}
	}

	return
}
// WriteTo writes the packet to the io.Writer.
WriteTo writes the packet to the io.Writer.
// WriteTo writes the packet to the io . Writer .
func (pkt *ConnectPacket) WriteTo(w io.Writer, version byte) (len int, err error) {
	var d int
	len, err = pkt.Header().WriteTo(w)
	if err != nil {
		return
	}
	d, err = writeString(w, pkt.Protocol)
	len += d
	if err != nil {
		return
	}
	d, err = w.Write([]byte{pkt.Version})
	len += d
	if err != nil {
		return
	}
	var flag byte
	if pkt.CleanSession {
		flag |= 0x02
	}
	if pkt.Will != nil {
		flag |= 0x04
		flag |= pkt.Will.QoS << 3
		if pkt.Will.Retain {
			flag |= 0x20
		}
	}
	if pkt.Auth != nil {
		flag |= 0x40 
		flag |= 0x80 
	}
	d, err = w.Write([]byte{flag})
	len += d
	if err != nil {
		return
	}
	d, err = writeInt(w, pkt.KeepAliveTimer)
	len += d
	if err != nil {
		return
	}
	d, err = writeString(w, pkt.ClientID)
	len += d
	if err != nil {
		return
	}
	if pkt.Will != nil {
		d, err = writeString(w, pkt.Will.Topic)
		len += d
		if err != nil {
			return
		}
		d, err = writeBytes(w, pkt.Will.Data)
		len += d
		if err != nil {
			return
		}
	}
	if pkt.Auth != nil {
		d, err = writeString(w, pkt.Auth.Username)
		len += d
		if err != nil {
			return
		}
		d, err = writeString(w, pkt.Auth.Password)
		len += d
		if err != nil {
			return
		}
	}
	return
}
func ( pkt * ConnectPacket ) WriteTo ( w io . Writer , version byte ) ( len int , err error ) { var d int len , err = pkt . Header ( ) . WriteTo ( w ) if err != nil { return } d , err = writeString ( w , pkt . Protocol ) len += d if err != nil { return } d , err = w . Write ( [ ] byte { pkt . Version } ) len += d if err != nil { return } var flag byte if pkt . CleanSession { flag |= 0x02 } if pkt . Will != nil { flag |= 0x04 flag |= pkt . Will . QoS << 3 if pkt . Will . Retain { flag |= 0x20 } } if pkt . Auth != nil { flag |= 0x40 flag |= 0x80 } d , err = w . Write ( [ ] byte { flag } ) len += d if err != nil { return } d , err = writeInt ( w , pkt . KeepAliveTimer ) len += d if err != nil { return } d , err = writeString ( w , pkt . ClientID ) len += d if err != nil { return } if pkt . Will != nil { d , err = writeString ( w , pkt . Will . Topic ) len += d if err != nil { return } d , err = writeBytes ( w , pkt . Will . Data ) len += d if err != nil { return } } if pkt . Auth != nil { d , err = writeString ( w , pkt . Auth . Username ) len += d if err != nil { return } d , err = writeString ( w , pkt . Auth . Password ) len += d if err != nil { return } } return }
WriteTo writes the packet to the io.Writer.
WriteTo writes the packet to the io . Writer .
// Username // PAssword