1+ const neffos = require ( 'neffos.js' ) ;
2+ const protos = require ( "./user_message_pb.js" )
3+
4+ var scheme = document . location . protocol === "https:" ? "wss" : "ws" ;
5+ var port = document . location . port ? ":" + document . location . port : "" ;
6+ var wsURL = scheme + "://" + document . location . hostname + port + "/echo" ;
7+
8+ var outputTxt = document . getElementById ( "output" ) ;
9+
10+ function addMessage ( msg ) {
11+ outputTxt . innerHTML += msg + "\n" ;
12+ }
13+
14+ function handleError ( reason ) {
15+ console . log ( reason ) ;
16+ window . alert ( reason ) ;
17+ }
18+
19+ function handleNamespaceConnectedConn ( nsConn ) {
20+ var username = prompt ( "Please specify a username: " ) ;
21+
22+ let inputTxt = document . getElementById ( "input" ) ;
23+ let sendBtn = document . getElementById ( "sendBtn" ) ;
24+
25+ sendBtn . disabled = false ;
26+ sendBtn . onclick = function ( ) {
27+ const input = inputTxt . value ;
28+ inputTxt . value = "" ;
29+ const userMsg = new protos . UserMessage ( ) ;
30+ userMsg . setUsername ( username ) ;
31+ userMsg . setText ( input ) ;
32+ userMsg . setAt ( new Date ( ) . toString ( ) ) ;
33+
34+ const body = userMsg . serializeBinary ( )
35+ // let msg = new neffos.Message();
36+ // msg.Namespace = "default";
37+ // msg.Event = "chat";
38+ // msg.Body = body;
39+ // msg.SetBinary = true;
40+ // nsConn.conn.write(msg);
41+ // ^ ==
42+ nsConn . emitBinary ( "chat" , body ) ;
43+ //
44+ // OR: javascript side will check if body is binary,
45+ // and if it's it will convert it to valid utf-8 text before sending.
46+ // To keep the data as they are, please prefer `emitBinary`.
47+ // nsConn.emit("chat", body);
48+ addMessage ( "Me: " + input ) ;
49+ } ;
50+ }
51+
52+ async function runExample ( ) {
53+ // You can omit the "default" and simply define only Events, the namespace will be an empty string"",
54+ // however if you decide to make any changes on this example make sure the changes are reflecting inside the ../server.go file as well.
55+ try {
56+ const conn = await neffos . dial ( wsURL , {
57+ default : { // "default" namespace.
58+ _OnNamespaceConnected : function ( nsConn , msg ) {
59+ addMessage ( "connected to namespace: " + msg . Namespace ) ;
60+ handleNamespaceConnectedConn ( nsConn ) ;
61+ } ,
62+ _OnNamespaceDisconnect : function ( nsConn , msg ) {
63+ addMessage ( "disconnected from namespace: " + msg . Namespace ) ;
64+ } ,
65+ chat : function ( nsConn , msg ) { // "chat" event.
66+ console . log ( msg ) ;
67+ const userMsg = protos . UserMessage . deserializeBinary ( msg . Body ) ;
68+ console . log ( "at: " , userMsg . getAt ( ) ) ;
69+ addMessage ( userMsg . getUsername ( ) + " at " + userMsg . getAt ( ) + " : " + userMsg . getText ( ) ) ;
70+ } ,
71+ chat_test : function ( nsConn , msg ) {
72+ addMessage ( new TextDecoder ( "utf-8" ) . decode ( msg . Body ) ) ;
73+ }
74+ }
75+ } ) ;
76+
77+ await conn . connect ( "default" ) ;
78+
79+ } catch ( err ) {
80+ handleError ( err ) ;
81+ }
82+ }
83+
84+ runExample ( ) . then ( r => {
85+ console . log ( r ) ;
86+ } ) ;
0 commit comments