Git Product home page Git Product logo

Comments (5)

atomatt avatar atomatt commented on May 20, 2024

Do you mean Stream.[sS]end()? If so, it's not quite as simple as making send() public as the Stream instance is hidden behind the an XMPP's In and Out channels.

I have no real objection to allowing a []byte stanza if it's useful. Do you have a particular use case for it? Note that it does give the application the ability to really mess up the underlying XML stream, but that's the application's fault ;).

from go-xmpp-atomatt.

remss avatar remss commented on May 20, 2024

My need is to provide in my component a "console mode" that sends custom XML stanza read from STDIN. Very useful to dev/debug.

Here the code to do that in my app:

go func() {
    fmt.Printf("Console = On\n")
    reader := bufio.NewReader(os.Stdin)
    lines := []string{}
    for {
        line, err := reader.ReadString('\n')
        if err != nil {
            fmt.Printf(err.Error())
            lines = []string{}
            continue
        }
        if line == "\n" { // empty line found, sends XML stanza !
            stanza := strings.Join(lines, "")
            lines = []string{}
            if strings.Trim(stanza, "\n") != "" {
                go stream.SendRaw([]byte(stanza))
            }
        } else {
            lines = append(lines, line)
        }
    }
}()

With this simple change in go-xmpp/src/xmpp/stream.go

func (stream *Stream) SendRaw(b []byte) error {
    return stream.send(b)
}

I agree with the application's responsibility in case of invalid stanza, but hey, "With great power comes great responsibility" ! :-)

from go-xmpp-atomatt.

atomatt avatar atomatt commented on May 20, 2024

Sorry for sitting on this for so long. I think you're right - it won't hurt to expose a method to send a complete stanza. However the current code is slightly wrong (mostly in terms of debug logging) at the moment. I'll see what I can do.

Just something I noticed about the above example … a XML stream is inherently sequential and xmpp.Stream does nothing to serialise access to the stream, so starting a new goroutine for each stanza - go stream.SendRaw([]byte(stanza)) - could result in things getting mixed up.

from go-xmpp-atomatt.

remss avatar remss commented on May 20, 2024

I see. We may change the Stream.Send() method to handle []byte type for v parameter in addition to classic stanza structures ? In this case the outgoing stanza flow will be preserved.

Below a patch proposal in go-xmpp/src/xmpp/stream.go

// Send a stanza. Used to write a complete, top-level element.
func (stream *Stream) Send(v interface{}) error {
    bytes, ok := v.([]byte)
    if ok {
        return stream.send(bytes)
    }
    if stream.config.LogStanzas {
        bytes, err := xml.Marshal(v)
        if err != nil {
            return err
        }
        return stream.send(bytes)
    }
    enc := xml.NewEncoder(stream.conn)
    return enc.Encode(v)
}

Then i could use comp.Out <- []byte(stanza) insted of go stream.SendRaw([]byte(stanza))

from go-xmpp-atomatt.

remss avatar remss commented on May 20, 2024

Opened issue for too long. I close it since I do not work with this library any more.

from go-xmpp-atomatt.

Related Issues (8)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.