Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

An exploration into a stand-alone library for Plug applications to easily adopt WebSockets.

License

NotificationsYou must be signed in to change notification settings

slogsdon/plug-web-socket

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build StatusCoverage StatusHex.pm Version

An exploration into a stand-alone library forPlug applications to easily adopt WebSockets.

Viewing the examples

Run these:

$ git clone https://github.com/slogsdon/plug-web-socket$ cd plug-web-socket$ mix deps.get$ iex -S mix run run_examples.exs

Go here:http://localhost:4000.

You will be presented with a list of possibleexamples/tests that use a WebSocket connection.

Integrating with Plug

If you're looking to try this in your own testapplication, do something like this:

defmoduleMyApp.RouterdousePlug.RouteruseWebSocket# WebSocket routes#      route     controller/handler     function & namesocket"/topic",MyApp.TopicController,:handlesocket"/echo",MyApp.EchoController,:echo# Rest of your router's plugs and routes# ...defrun(opts\\[])dodispatch=dispatch_table(opts)Plug.Adapters.Cowboy.http__MODULE__,opts,[dispatch:dispatch]endend

For the time being, there is arun/1 functiongenerated for your router that starts a HTTP/WSlistener. Not sure if this will stay or getreduced to helper functions that aid in thecreation of a similar function. Most likely thelatter will win out to help compose functionality.The big part that it plays is the building of adispatch table to pass as an option to Cowboy thathas an entry for each of your socket routes and acatch all for HTTP requests.

Add the necessary bits to a module

From the topic example:

defmoduleMyApp.TopicControllerdodefhandle(:init,state)do{:ok,state}enddefhandle(:terminate,_state)do:okenddefhandle("topic:"<>letter,state,data)dopayload=%{awesome:"blah#{letter}",orig:data}{:reply,{:text,payload},state}endend

Currently, the function name needs to be uniqueacross all controllers/handlers as its used forthe Events layer.

Broadcast from elsewhere

Need to send data out from elsewhere in your app?

# Build your messagetopic="my_event"data=%{foo:"awesome"}mes=WebSocket.Message.build(topic,data)json=Poison.encode!(mes)# Pick your destination (from your routes)name=:handle# Send away!WebSockets.broadcast!(name,json)

This needs to be nicer, but this is still inprogress.

License

WebSocket is released under the MIT License.

SeeLICENSE for details.

About

An exploration into a stand-alone library for Plug applications to easily adopt WebSockets.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors2

  •  
  •  

[8]ページ先頭

©2009-2025 Movatter.jp