今天将Go 的websocket集成到已有的http中时也遇到以下一个问题:其中主要是接口类型转换

转载自[http://www.cnblogs.com/ghj1976/archive/2013/04/23/3037914.html]


我们用Golang做网站时,必然会有这样的代码:

http.Handle("/", http.FileServer(http.Dir(".")))

注意这里的 http.Handle( 函数的第二个参数,Golang的源码中这个函数的描述如下:

// Handle registers the handler for the given pattern

// in the DefaultServeMux.

// The documentation for ServeMux explains how patterns are matched.

func Handle(pattern string, handler Handler) { DefaultServeMux.Handle(pattern, handler) }

其中第二个参数是一个 interface,必须实现ServeHTTP(ResponseWriter, *Request)方法

// Objects implementing the Handler interface can be

// registered to serve a particular path or subtree

// in the HTTP server.

//

// ServeHTTP should write reply headers and data to the ResponseWriter

// and then return.  Returning signals that the request is finished

// and that the HTTP server can move on to the next request on

// the connection.

type Handler interface {

ServeHTTP(ResponseWriter, *Request)

}

如果我们用WebSocket时,又会发现, 我们给http.Handle( 传递的是 websocket.Handler(。

http.Handle("/socket", websocket.Handler(Echo))

今天在将WebSocket集成到已有的http

websocket.Handler 又是另外一个接口,如下

// Handler is an interface to a WebSocket.

type Handler func(*Conn)

这里为啥会出现接口更换了? 其实 再往下看可以看到 websocket.Handler 接口多一个公共的方法:ServeHTTP, 即接口 websocket.Handler 实现了 http 的 Handler 接口。

// ServeHTTP implements the http.Handler interface for a Web Socket

func (h Handler) ServeHTTP(w http.ResponseWriter, req *http.Request) {

这里的interface有了其他语言类的一些特征,比较有趣。

// Handler is an interface to a WebSocket.type Handler func(*Conn)
// ServeHTTP implements the http.Handler interface for a Web Socketfunc (h Handler) ServeHTTP(w http.ResponseWriter, req *http.Request)


type websocket.Handler func(*websocket.Conn)

Handler is a simple interface to a WebSocket browser client. It checks if Origin header is valid URL by default. You might want to verify websocket.Conn.Config().Origin in the func. If you use Server instead of Handler, you could call websocket.Origin and check the origin in your Handshake func. So, if you want to accept non-browser client, which doesn't send Origin header, you could use Server . that doesn't check origin in its Handshake.

  • method

    (websocket.Handler).ServeHTTP(w net/http.ResponseWriter, req *net/http.Request)

ServeHTTP implements the http.Handler interface for a WebSocket

通过以前的信息可以看出websocket.Handler实现了http.Handler接口,从而实现了接口类型的转换。

Go 接口转换的一个例子的更多相关文章

  1. 一个vue请求接口渲染页面的例子

    new Vue({ el:'#bodylist', data: { list: [ { "type_id": "1", "type_name" ...

  2. 对Jena的简单理解和一个例子

    本文简单介绍Jena(Jena 2.4),使用Protégé 3.1(不是最新版本)创建一个简单的生物(Creature)本体,然后参照Jena文档中的一个例子对本体进行简单的处理,输出本体中的Cla ...

  3. spring笔记--使用springAPI以及自定义类 实现AOP的一个例子

    Spring的另一个重要思想是AOP,面向切面的编程,它提供了一种机制,可以在执行业务前后执行另外的代码,Servlet中的Filter就是一种AOP思想的体现,下面通过一个例子来感受一下. 假设我们 ...

  4. 从一个例子中体会React的基本面

    [起初的准备工作] npm init npm install --save react react-dom npm install --save-dev html-webpack-plugin web ...

  5. Erlang 程序引发共享内存 bug 的一个例子

    虽然 Erlang 的广告说得非常好,functional.share-nothing.消息传递,blah blah 的,好像用 Erlang 写并发程序就高枕无忧了,但是由于 Erlang 信奉高度 ...

  6. Spark小课堂Week7 从Spark中一个例子看面向对象设计

    Spark小课堂Week7 从Spark中一个例子看面向对象设计 今天我们讨论了个问题,来设计一个Spark中的常用功能. 功能描述:数据源是一切处理的源头,这次要实现下加载数据源的方法load() ...

  7. Webpack入门——使用Webpack打包Angular项目的一个例子

    2016.1.22,对大多数人来说,这是一个非常平常的日子,但这却是我决定在博客园写博客的日子.虽然注册博客园的博客已有4年8个月,却一直没有动手写过一篇博客,原因是觉得自己水平不行,写不出好东西,所 ...

  8. Web服务,XFire的一个例子

    Web服务优点 互操作性:实现不同系统间的相互调用(语言无关.平台无关) Web服务是什么 Web 服务是一类应用程序,是能够用编程的方法通过Web调用来实现某个功能的应用程序 Web服务的体系结构 ...

  9. Java编程思想中关于闭包的一个例子

    Java编程思想中的一个例子,不是很理解使用闭包的必要性,如果不使用闭包,是不是有些任务就不能完成?继续探索. package InnerClass; interface Incrementable ...

随机推荐

  1. Codeforces Round #329 (Div. 2) A. 2Char 暴力

    A. 2Char Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/593/problem/A De ...

  2. Struts2 学习笔记 11 Result part2

    之前学习了result type 和global result 我们现在来说一下 1.Dynamic Result动态结果集.先来看一下小项目的目录 首页的两个链接访问user/user?type=1 ...

  3. 调用Dll里面的窗体

    将窗体资源分装到DLL中并且调用 用Delphi生成DLL并封装窗体的示例 调用Dll里面的窗体 DLL文件 library Project2;{ Important note about DLL m ...

  4. Servlet---JavaWeb技术的核心基础,JavaWeb框架的基石(一)

    初学JavaWeb开发,请远离各种框架,从Servlet开始.         Web框架是开发者在使用某种语言编写Web应用服务端是关于架构的最佳实践.很多Web框架是从实际的Web项目抽取出来的, ...

  5. 元数据标签Embed

    关于Embed外部资源的使用方法总结 Flex软件中经常需要使用一些外部的资源,如图片.声音.SWF或字体,虽然你也可以在软件运行的时候引入和载入,但是也可能经常需要直接将这些资源编译(Compile ...

  6. Spark_Api_图解

  7. Eclipse的java代码出错:The import org.apache cannot be resolved

    Eclipse中,折腾java代码. 把之前在android中的代码拿过来使用. 结果出现The import org.apache cannot be resolved的错误: [解决过程] 1.这 ...

  8. 网口扫盲三:以太网芯片MAC和PHY的关系

    转载:http://www.cnblogs.com/jason-lu/articles/3195473.html   问:如何实现单片以太网微控制器? 答:诀窍是将微控制器.以太网媒体接入控制器(MA ...

  9. UITableView优化技巧

    UITableView的简单认识 UITableView最核心的思想就是UITableViewCell的重用机制.简单的理解就是:UITableView只会创建一屏幕(或一屏幕多一点)的UITable ...

  10. android开发之路13(实际开发常见问题及解决办法ING)

    1.DDMS下无法浏览SDcard? 在做音乐播放器的时候,想通过eclipse中的DDMS视图将音频文件push到sd卡中,发现总是报错 :Failed to push selection: Rea ...