Golang WebSocket Summary (problem sticker)

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

because of the need for work, contact with the WebSocket, the beginning of some very simple code, do not know how to run up, so, there is always a layer of mystery, but not much interest to study it. However, it is unavoidable to understand some of the work will not affect the follow-up. From the collapse of the heart to learn, to the final completion of the coding test, not more than a week, encountered a lot of problems, fortunately, through their own thinking and the help of teachers have been solved.

On the go and WebSocket programming, GitHub has a lot of open source sources, such as Https://github.com/garyburd/go-websocket. I found that the online websocket programming about Go is basically about the same type, and of course, WebSocket is just that. And again, for such a simple question, the trouble I have come across.

1. How do I run the server after it starts? Because the beginning of the understanding of WebSocket is smattering, even the program how to run without knowing. The start is to run the HTML file directly, just static pages, seemingly no interaction with the server at all. Think, directly in the browser input IP, unexpectedly good, this just understand the meaning of the URL specified in the server.

      2. Because of the work required, our development is carried out in the virtual machine, but the virtual machine can not be connected to the external network. I think or out of the virtual machine to use more than better, so, every morning to take pains to deploy the environment, is for the day can work outside the network environment, this is how fearless spirit ah, hey. At the same time, outside the network to run a good program, the migration to the intranet when it is not running up. This reason is also very easy to find, because there is only one difference between the intranet and the outside network, one can sisu net, one cannot. Read the code, there is src= "http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js", so, these JS resources should be downloaded on the Internet, I downloaded the library and saved it to a file, and the resource was linked through SRC. The same operation, I got out of the net again, but also the operation of the department up. Previously learned some browser debugging methods, found that they can not find these source files, the directory is good, how can not find it? That's really depressing. Call Master to come over, he said can not find the resources, and then the principle of websocket to me again, these agreements and so on before the contact is too little, many times it is not clear at once. said in the browser input IP, will be from this directory down, of course, can not find. Then say open nginx What, anyway I didn't understand. Since I can only find this HTML file, why can't I write the contents of these JS files directly into HTML? It turns out that this idea is good, because the JS code is relatively centralized and does not affect the readability of the code.

3. With regard to the transmission of information in the agreement, it is important to unify the data formats at both ends. My idea is to get the data from the end of the line and encapsulate it according to my own needs. This way, both sides are passed in a byte array. However, after the server received the data, in order to verify the correctness, I converted it to a string to print out, it is strange, as long as a conversion, you can only print out the first time to receive the data, the back seems to have been discarded the same. If you do not convert, you can print out the binary stream of information in a lot of way. I swept all the wrong places and didn't think there was any problem. I called the Master again. Describing my situation, the advice he gave me was to try to use the WebSocket package already encapsulated functions, so that on the one hand the performance is guaranteed, on the other hand can also reduce the probability of error. I was still very tangled. Why this happens, so even if you replace your own function with functions in the library, always do not forget to try to change the time is still a problem. For debugging convenience, put all the input into the redirected file and then look at the results through a text editor. This time, this is the same result, which means that my code may not be the source of the problem. I put the output as far as possible, in the compiler to run directly, unexpectedly see the correct results, I confirm that the problem is the text editor, is really not reliable ah, I spent an afternoon to find the problem. Later also encountered a similar problem, all the output in the text editor is actually a string of numbers, practical cat printing, the content is normal. Do not own a word, this open source is really not reliable ah. Of course, now will not be so dependent on the text editor, in fact, has changed three, there are always annoying bugs occasionally.

4. With regard to the organizational structure of the Code, I feel that it is impossible for the master to solve a sentence, really admire my pleasantly surprised ah. Sometimes, some functions encapsulated into a function, and then make the call, you can make the code look neat, I made this problem, always think of those functions encapsulated, direct call, very convenient. About the function embedded inside, but have seen, I have never used, and now encounter problems, it is impossible to think that can be solved ah. So, it's also an experience.

5. Chinese garbled problem. For the people who learn Java, Chinese garbled seems to be a commonplace problem, no matter how careful you are, this Chinese garbled always haunted. Before, every time you encounter garbled, you will complain about yourself, how to choose Java, this is not nothing to find out? Now finally out of Java, unexpectedly also encountered Chinese garbled. I actually bargain with the master, we can not support the Chinese Ah, why do not have to spend the effort to solve the problem. He had a green face and said it had to be. Now think about it, I am very funny, how so easy to retreat. When using English, TCP and WebSocket communicate with each other is good, only in the WebSocket browser client input Chinese time is garbled. Tracking for a long time, the discovery from the browser, to send out from the server is the correct Chinese, only to the browser after receiving the data is garbled. I will boil down to the question, is there a problem with character encoding in JS code? At that time did not feel that their server send the end is problematic, because even if the websocket between the use of program communication, Chinese is not a problem. To find the former master, he repeatedly asked me to confirm that the server-side send is normal, I said OK, and then he asked, turned into a no problem it. He started looking for the front-end problem. said that the front-end code is self-adaptive, should not adjust the character encoding, otherwise the user experience is poor, who because access to a Web page also to modify the character set AH. Finally came to the conclusion that the browser received the message when it is garbled. This is not fun, it is fully explained that the problem is in the sending side, then why not use the browser is good? With the original demo program to run, found that the Chinese is possible, find the next, the original I sent the message is converted to a byte array, in fact, directly according to the data in string format to the function can be. Maybe the data has been re-converted inside the function.

These should be the most time-obsessed problems, and by solving these problems, I have learned a lot of things. The problem can be analyzed in a calm mind.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.