Skip to content

Commit

Permalink
TODO
Browse files Browse the repository at this point in the history
  • Loading branch information
Jorropo committed Mar 16, 2023
1 parent ed96a64 commit 130a18e
Show file tree
Hide file tree
Showing 4 changed files with 607 additions and 0 deletions.
13 changes: 13 additions & 0 deletions examples/chat-wasm-browser-with-rendezvous/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<html>
<head>
<meta charset="utf-8"/>
<script src="wasm_exec.js"></script>
<script>
const go = new Go();
WebAssembly.instantiateStreaming(fetch("main.wasm"), go.importObject).then((result) => {
go.run(result.instance);
});
</script>
</head>
<body><p id="output"></p></body>
</html>
40 changes: 40 additions & 0 deletions examples/chat-wasm-browser-with-rendezvous/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//go:build js

package main

import (
"fmt"
"strings"
"sync"
"syscall/js"

"github.com/libp2p/go-libp2p"
"github.com/libp2p/go-libp2p/core/network"
)

func main() {
var text strings.Builder
var textLock sync.Mutex
g := js.Global()
out := g.Get("output")

h, err := libp2p.New()
if err != nil {
panic(err)
}

h.SetStreamHandler("/chat/1.0.0", func(s network.Stream) {
defer s.Close()
var err error
for err != nil {
var buf [1024]byte
var n int
n, err = s.Read(buf[:])
textLock.Lock()
text.Write(buf[:n])
out.Set("innerText", text.String())
textLock.Unlock()
}
fmt.Println(err)
})
}
Binary file not shown.
Loading

0 comments on commit 130a18e

Please sign in to comment.