2

I am trying to read binary data in javascript. I got one BinaryReader.js on net. I copied that file where Default.aspx is. If I write the code like following then project got build and running but handshake is not getting done. If I remove src="BinaryReader.js" then handshake is getting done properly. So my question is, Can I use external script along with my script in same asp.net page? If yes then what am i doing wrong?

<script src="BinaryReader.js" language="javascript" type = "text/javascript">
    var ws;
    function btnConnectSend_onclick() {
        if ("WebSocket" in window) {
            ws = new WebSocket("ws://localhost:35000/");
            ws.onopen = function() {
                alert("Connection Open......");
            };
            ws.onmessage = function(evt) {
                var reader = new BinaryReader(evt.data);
                var tag = reader.readString(26);
                //var txt = document.createTextNode(evt.data.toString());
                form1.txtMessage.appendChild(tag);
            };
            ws.onclose = function() {
                alert("Socket Closed!!!");
            };

            ws.onerror = function() {
                alert("WTF!");
            };
        }
    }

    function btnClose_onclick() {
        ws.close();
    };

</script>

2 Answers 2

1

No, you have to create a separate script block for your code, or put it in the script file.

<script src="BinaryReader.js" language="javascript" type = "text/javascript"></script>
<script language="javascript" type = "text/javascript">
    var ws;
    function btnConnectSend_onclick() {
        if ("WebSocket" in window) {
            ws = new WebSocket("ws://localhost:35000/");
            ws.onopen = function() {
                alert("Connection Open......");
            };
            ws.onmessage = function(evt) {
                var reader = new BinaryReader(evt.data);
                var tag = reader.readString(26);
                //var txt = document.createTextNode(evt.data.toString());
                form1.txtMessage.appendChild(tag);
            };
            ws.onclose = function() {
                alert("Socket Closed!!!");
            };

            ws.onerror = function() {
                alert("WTF!");
            };
        }
    }

    function btnClose_onclick() {
        ws.close();
    };

</script>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot! reader.readString() unction call is not getting executed though. But i got answer for question posted here
0

this will add it to the page response, it will be included in the page, so that you can use it, then create one more block of script tag, and then call the functions from there.

<script src="BinaryReader.js" language="javascript" type = "text/javascript"></script>

<script type="text/javascript"> 
 /// put your all Js Code of btnsendClick
</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.