Skip to content
Permalink
245844dab4
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
63 lines (51 sloc) 6.2 KB
# example-server
## Intro
Included in this repository is the file `/sender/sender.go` which will attempt to make a websocket connection
and run multiple senders that will each send packets to the websocket. These packets consist of an ID, a number
0, 1, or 2 which is a number of seconds to wait, and a timestamp. Your job is to build a server that will receive
the packets via webosocket, wait the requested amount of time, and then send a response (one response per packet).
You should read through sender.go and try to understand what is going on -- I will be around for questions no matter
how trivial.
##Shared data types
The data types for sending and receiving data are in `/data-types/data-types.go`. The sender uses the `DataStruct`
to send data and its listeners accept `ResponseStructs`, so your server should also use these data types to
process and send its messages.
##Build configurations
You will need to create two different build configurations -- one for the sender and one for your server.
Each should have the run type set to file, and then just give it the path to the .go file you want to build.
Make the output and working directory the same directory with the file you're building, i.e. add `/sender` for the sender
(and put your server in its own directory. When I did this I named it responder).
##Getting started
Just to reiterate what I said in slack:
I needed to use mutexes and waitgroups for this. These are in the "sync" package. Often times I will use the words
lock and mutex interchangeably because mutexes are locks that can only be opened by one lock request a time.
I also had to use `gorilla/websockets`, which hopefully you should all have properly installed from our meeting
Wednesday 12/5. Setting up the websocket requires use of the "net/http" package which is part of the core library
(so you don't have to install it). I also used the "time" package, and of course I imported the `data-types.go` file
so I could use those for my server. Here's my complete import statement in my solution:
import (
"fmt"
"generator-server/data-types"
"github.com/gorilla/websocket"
"net/http"
"sync"
"time"
)
Some thoughts to direct your initial planning --
- structs are a great way to keep things together. If you're gonna have a loop that writes to a websocket and you
need to manage a lock for that websocket, it might be a good idea to keep a websocket and a lock for it in a struct
- this pattern is great for handling messages from multiple channels
```
for {
select {
case data, ok := <-c: // c and d are channels
# do something
case data, ok := <-d:
# do something
}
}
```
- Make sure you lock before writing to a websocket so that you don't get a concurrent write error
- If your server quits too early it might be because of waitgroups -- Make sure you actually wait for waitgroups
and add goroutines you want to wait for to them.
- Ask any questions you need to, my slack pm is always open and I'll try to help you out as soon as I can.