-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
607 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
Oops, something went wrong.