Use Golang to develop WebSocket records

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

It has been the development of restful APIs throughout the community using the Beego framework, and the use of Github.com/gorilla/websocket as a websocket package after finding information

Defining the properties of a WebSocket

var upgrader = WebSocket. Upgrader{readbuffersize:  1024,writebuffersize:1024,}

are based on official examples, and also refer to an example of a chat room in Beego.

WS, Err: = Upgrader. Upgrade (this. Ctx.responsewriter, this. Ctx.request, Nil)
Defer ws. Close ()
Because the Beego framework is used, the transfer parameters are obtained using Beego when the WebSocket is instantiated.

After the WS is instantiated, A for loop is used, waiting for the data to be received, and processing the data

_, p, err: = ws. Readmessage ()
I did not judge the content here, so the first parameter, the returned data type, I dropped directly, because the function is very simple, so do not do data type validation

After a heap of data is processed, the JSON is returned

If Err = ws. Writejson (RESMAP); Err! = Nil {fmt. PRINTLN (ERR) ws. Close () Break}
Test convenience, the error message directly out, WS package with the return of JSON, direct map to pass over it, if the error must be direct ws. Close () will be overwhelmed by background information

In fact, the use is very simple, the trouble is that the company's Nginx version is too low, when the deployment of the first upgrade nginx, and then do a reverse proxy, listening to the script port,

Set the lifetime of the CGI, otherwise it is based on PHP settings, so separate the WebSocket program to increase the duration of the segment


Because there is a delay in the logic of the function, the beginning is to use a time.sleep to achieve, and later read the Go language programming, learned a trick, you can use channel to do processing.


How to set Nginx

Because deployed on the PHP server, so directly using SUPERTCTL to do a process monitoring, let go as a background process to run, and then use Nginx to do a reverse proxy, set the following


First of all. Modify the nginx.conf file to add the configuration in HTTP, that is, with the server peer


Map $http _upgrade $connection _upgrade {    default upgrade;    ' close;} #limit_zone  crawler  $binary _ REMOTE_ADDR  10m;upstream websocket {    server 127.0.0.1:8198;}

Set up reverse proxy I'm listening to the native 8198 port

Then set the Access directory

location/backapi/{                        proxy_set_header Host $host;            Proxy_pass Http://websocket;            Proxy_http_version 1.1;            Proxy_read_timeout 86400s;            Keepalive_timeout 86400s;            #proxy_redirect off;            Proxy_set_header Upgrade $http _upgrade;            Proxy_set_header Connection "Upgrade";            #proxy_set_header x-forwarded-for $proxy _add_x_forwarded_for;                  }
When Nginx gets access to the directory, it calls the go script to parse it and sets the read time to 86,400 seconds to prevent the ngxin from automatically shutting down the client.



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.