今天将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. Lambda表达式实现有限状态机

    实现状态机有多种模式,其中最灵活而强大的方式是通过迁移表来实现,该方式的缺点之一是需要编写大量小块代码去支持迁移表.而在C#3.0中,可以以一种非常优雅的方式实现. 除了有限状态机外,还有有限自动机, ...

  2. On Memory Leaks in Java and in Android.

    from:http://chaosinmotion.com/blog/?p=696 Just because it's a garbage collected language doesn't mea ...

  3. Android-L-Samples

    https://github.com/s3xy4ngyc/Android-L-Samples

  4. Android进阶2之APK方式换肤

    public class MainActivity extends Activity { private Button defaultbutton = null; @Override public v ...

  5. UEditor-JSP版部署说明

    昨天项目中需要在UEditor用到图片上传功能,于是昨天中午便开始做这一块的整合,一不小心就弄到了现在, 坑还挺多的,比如一开始一直报ActionEnter cannot be resolved to ...

  6. 文件尾存在EOF吗?

    参考:http://bbs.csdn.net/topics/290027166 我們先一起來看看FILE是怎么定義的:   FILE                          <STDI ...

  7. 网卡及MAC和PHY的区别

    转载:http://blog.sina.com.cn/s/blog_53d7350f0100mudb.html 一块以太网网卡包括OSI(开方系统互联)模型的两个层.物理层和数据链路层.物理层定义了数 ...

  8. 小白日记26:kali渗透测试之提权(六)--收集敏感信息,隐藏痕迹

    提权后操作 提权之后,要收集目标系统的重要信息LINUX /etc/resolv.conf       #查看DNS配置,是否可以进行DNS篡改或劫持 /etc/passwd         #存放账 ...

  9. javascript 通过面向对象编写圆形数字时钟

    效果如图所示,代码如下 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&qu ...

  10. [设计模式]<<设计模式之禅>>关于开闭原则

    开闭原则是Java世界里最基础的设计原则,它指导我们如何建立一个稳定的.灵活的系统,先来看开闭原则的定义: Software entities like classes,modules and fun ...